XML验证 - 多个错误&行号 [英] XML Validation - multiple errors & line numbers

查看:88
本文介绍了XML验证 - 多个错误&行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我可以使用以下两种方法之一对模式验证xml文件:


1)创建一个XmlReader并使用.Validate方法


2)创建一个XmlReader并在执行.Read方法时循环。


它们都可以工作,但是在不同的方法。使用方法1)我得到一个错误,并且可以通过使用XmlSchemaValidationException来识别它是哪一行以及它在该行上的位置。使用方法2)我得到所有的错误,但我没有得到行号或有用的
之类的东西,因为没有引发XmlSchemaValidationException,我必须使用  XmlSchemaException。


我基本上想要结合两种方法的功能,即我希望能够列出所有错误并通过线和位置识别它们的位置。如果有必要,我会接受行...


干杯,


 


Rob


 


 


 

解决方案

如果您有一个XML文件并想要验证它,那么使用XmlReaderSettings实例所在的XmlReader.Create("file.xml",XmlReaderSettingsInstance)设置验证XmlReader使用所有必需的属性创建(例如验证类型
作为模式,添加所有模式,ValidationEventHandler设置,启用了ReportWarnings等ValidationFlags)然后使用while(reader.Read()){}外观使用XmlReader是首选方法。您应该能够通过将
XmlReader强制转换为IXmlLineInfo来访问行号。还要检查异常是否有内部异常或者是否可以转换为您要查找的类型。


对于Validate方法,这些方法存在于XmlDocument(DOM)和XDocument(LINQ to XML)中,如果您希望它们报告多个错误,那么它们会有一个重载,它会使ValidationEventHandler处理多个错误,例如
http://msdn.microsoft.com/en-us/library /bb340331.aspx ,如果确保将该参数设置为ValidationEventHandler,那么我确信它会被调用而不是仅仅
在第一个错误上抛出异常。


Hi,

I can validate an xml file against a schema using one of 2 methods:

1) Create an XmlReader and use the .Validate method

2) Create an XmlReader and loop around whilst performing a .Read method.

They both work, but in different ways. With method 1) I get one error and can identify which line it is and where on that line it occurred by using the XmlSchemaValidationException. With method 2) I get all the errors, but I don't get line numbers or useful stuff like that as no XmlSchemaValidationException are raised, I have to use  XmlSchemaException.

I basically want to combine the functionality of both methods, i.e. I want to be able to list all the errors and identify where they are by line and position. I'll accept just line if necessary...

Cheers,

 

Rob

 

 

 

解决方案

If you have an XML file and want to validate it then setting up a validating XmlReader by using XmlReader.Create("file.xml", XmlReaderSettingsInstance) where the XmlReaderSettings instance has been created with all necessary properties (i.e. validation type as schema, all schemas added, ValidationEventHandler set, ValidationFlags like ReportWarnings enabled) and then consuming the XmlReader with a while (reader.Read()) {} look is the preferred approach. You should be able to access line numbers by casting the XmlReader to IXmlLineInfo. Also check whether exceptions have inner exceptions or are castable to the type you are looking for.

As for a Validate method, these exist on XmlDocument (DOM) and XDocument (LINQ to XML), if you want them to report several errors then they have an overload that takes a ValidationEventHandler to handle several errors e.g. http://msdn.microsoft.com/en-us/library/bb340331.aspx, if you make sure you set that argument to your ValidationEventHandler then I am sure it will get called instead of just throwing an exception on the first error.


这篇关于XML验证 - 多个错误&行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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