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

时间:2010-10-22 17:14:40

标签: c# xml string xsd

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

3 个答案:

答案 0 :(得分:23)

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

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

答案 1 :(得分:11)

您可以使用StringReader

string content = ".......";
XmlSchema schema = new XmlSchema();
schema.Read(new StringReader(content), ValidateSchema);

答案 2 :(得分:1)

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
});