使用XSD架构的XML验证 [英] Xml validation using XSD schema

查看:168
本文介绍了使用XSD架构的XML验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code帮我验证XML文件,XSD架构。

  XmlReaderSettings设置=新XmlReaderSettings();
settings.Schemas.Add(NULL,xsdFilePath);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler + =新System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);
XmlDocument的文档=新的XmlDocument();
document.Load(xmlFilePath);
RDR的XmlReader = XmlReader.Create(新StringReader(document.InnerXml),设置);而(rdr.Read())
{}
的isValid = TRUE;

本的ValidationEventHandler还告诉我的错误是什么,但并没有告诉我的上线它们所在',其中'或。有没有什么办法让在XML无法进行验证的行号?


解决方案

这是不是你所追求的?


  

创建一个 XmlReaderSettings 对象,并通过该对象能够警告。


  
  

不幸的是,似乎没有办法自己的 XmlReaderSettings 对象传递给 XmlDocument.Validate()。结果
  相反,你可以使用验证的XmlReader XmlNodeReader对象来验证一个现有的的XmlDocument (使用 XmlNodeReader对象 StringReader 而非的XmlDocument


  XmlDocument的X =新的XmlDocument();
x.LoadXml(XmlSource);XmlReaderSettings设置=新XmlReaderSettings();
settings.CloseInput = TRUE;
settings.ValidationEventHandler + =处理程序;settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(NULL,ExtendedTreeViewSchema);
settings.ValidationFlags =
     XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;StringReader R =新StringReader(XmlSource);使用(的XmlReader validatingReader = XmlReader.Create(R,设置)){
        而(validatingReader.Read()){/ *刚刚经历文件*循环/}
}

和处理程序:

 私有静态无效处理程序(对象发件人,ValidationEventArgs E)
{
        如果(e.Severity == || XmlSeverityType.Error == e.Severity XmlSeverityType.Warning)
          System.Diagnostics.Trace.WriteLine(
            的String.Format(行:{0},位置:{1} \\{2} \\,
                e.Exception.LineNumber,e.Exception.LinePosition,e.Exception.Message));
}

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

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?

解决方案

Would not this do what you are after ?

Create an XmlReaderSettings object and enable warnings through that object.

Unfortunately, there seems to be no way to pass your own XmlReaderSettings object to XmlDocument.Validate().
Instead, you can use a validating XmlReader and an XmlNodeReader to validate an existing XmlDocument (using a XmlNodeReader with StringReader rather than 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));
}

这篇关于使用XSD架构的XML验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆