How to get XmlSchema object from XSD which is string in C#?

Viewed 14201

How to get XmlSchema object from large string that contains all XSD content?

3 Answers

The Read method is static. So better use

XmlSchema schema = XmlSchema.Read(
    schemaReader, (sender, args) =>
    {
         // HANDLE VALIDATION FAILED
    });                                                                        
string xsdContent = "...";
string xmlContent = "...";

XmlSchemaSet schema;
XDocument xmlDoc;
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xsdContent)))
{
    var xsc = XmlSchema.Read(ms, (o, e) =>
    {
        Error.SetWarning($"XML Schema error: {e.Message}");
    });
    schema = new XmlSchemaSet();
    schema.Add(xsc);
    xmlDoc = XDocument.Parse(xmlContent, LoadOptions.SetLineInfo);
}

xmlDoc.Validate(schema, (o, e) =>
{
    // handle validation errors
});
Related