简体   繁体   中英

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

如何从包含所有 XSD 内容的大字符串中获取 XmlSchema 对象?

The Read method is static. So better use

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

You can use a StringReader :

string content = ".......";
XmlSchema schema = new XmlSchema();
schema.Read(new StringReader(content), ValidateSchema);
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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM