繁体   English   中英

如何从 C# 中的字符串 XSD 获取 XmlSchema 对象?

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

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

Read 方法是静态的。 所以更好地使用

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

您可以使用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
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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