C# 使用 XSD 架构的 Xml 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/572853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:45:52  来源:igfitidea点击:

Xml validation using XSD schema

c#validationxsd

提问by Elroy

The following code helps me validate an XML file with an XSD schema.

以下代码帮助我验证具有 XSD 架构的 XML 文件。

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, xsdFilePath);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);
XmlDocument document = new XmlDocument();
document.Load(xmlFilePath);
XmlReader rdr = XmlReader.Create(new StringReader(document.InnerXml), settings);

while (rdr.Read())
{

}
isValid = true;

The ValidationEventHandler also tells me what the errors are, but doesn't tell me on 'where' or 'on which line' they are located. Is there any way to get the line number where the XML fails to be validated?

ValidationEventHandler 还告诉我错误是什么,但没有告诉我它们位于“哪里”或“在哪一行”。有什么方法可以获取无法验证 XML 的行号吗?

采纳答案by VonC

Would not this do what you are after ?

这不会做你想要的吗?

Create an XmlReaderSettingsobject and enable warnings through that object.

Unfortunately, there seems to be no way to pass your own XmlReaderSettingsobject to XmlDocument.Validate().
Instead, you can use a validating XmlReaderand an XmlNodeReaderto validate an existing XmlDocument(using a XmlNodeReaderwith StringReaderrather than an XmlDocument)

创建一个XmlReaderSettings对象并通过该对象启用警告。

不幸的是,似乎没有办法将您自己的XmlReaderSettings对象传递给XmlDocument.Validate().
相反,您可以使用验证XmlReader和 anXmlNodeReader来验证现有XmlDocument(使用XmlNodeReaderwithStringReader而不是 an XmlDocument

XmlDocument x = new XmlDocument();
x.LoadXml(XmlSource);

XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;     
settings.ValidationEventHandler += Handler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, ExtendedTreeViewSchema);
settings.ValidationFlags =
     XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation ;

StringReader r = new StringReader(XmlSource);

using (XmlReader validatingReader = XmlReader.Create(r, settings)) {
        while (validatingReader.Read()) { /* just loop through document */ }
}

And the handler:

和处理程序:

private static void Handler(object sender, ValidationEventArgs e)
{
        if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
          System.Diagnostics.Trace.WriteLine(
            String.Format("Line: {0}, Position: {1} \"{2}\"",
                e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.Message));
}

回答by Richard

ValidationEventArgs.Message includes line/column in its text.

ValidationEventArgs.Message 在其文本中包含行/列。

ValidationEventArgs.Exception has fields for line and column.

ValidationEventArgs.Exception 具有用于行和列的字段。