如何在 VB.net 中根据 Schema 验证 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15088585/
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
How to validate XML against Schema in VB.net
提问by lawphotog
I am trying to validate xml against a schema. I am using XmlReaderSetting and trying to follow an example on MSDN but not able to make it work. It doesn't validate the xml even I throw a totally different file against schema. Can anyone explain me what I am missing?
我正在尝试根据架构验证 xml。我正在使用 XmlReaderSetting 并尝试遵循 MSDN 上的示例,但无法使其工作。即使我针对架构抛出一个完全不同的文件,它也不会验证 xml。谁能解释我缺少什么?
Thanks,
谢谢,
Protected Sub ValidateXML(xmlFilePath As String, schemasFilePath As String)
Try
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("http://www.w3.org/2001/XMLSchema", schemasFilePath)
settings.ValidationType = ValidationType.Schema
Dim reader As XmlReader = XmlReader.Create(xmlFilePath, settings)
Dim document As XmlDocument = New XmlDocument()
document.Load(reader)
Dim eventHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
' the following call to Validate succeeds.
document.Validate(eventHandler)
reader.Close()
Catch ex As Exception
Messagebox(ex.Message, "error")
End Try
End Sub
Protected Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
'Messagebox(e, "error")
Case XmlSeverityType.Warning
'Messagebox(e, "error")
End Select
End Sub
回答by Steven Doggart
You are mixing two different ways to read the XML file. You are using an XmlReaderobject and an XmlDocumentobject. Typically, you'd only use one or the other. It will work to use both, as you have done, but it does introduce some unnecessary confusion.
您正在混合两种不同的方式来读取 XML 文件。您正在使用一个XmlReader对象和一个XmlDocument对象。通常,您只会使用其中一种。正如您所做的那样,两者都可以使用,但它确实会引入一些不必要的混乱。
The reason the validation is not working is because you are adding the schema validation to the reader, but then you attach the ValidationEventHandlermethod to the XmlDocumentobject. Both XmlDocumentand XmlReaderare capable of performing schema validation, and they each have their own XmlSchemaSetand validation event handler that they use to perform the the validation. You have given half of what they need to each of them instead of all of what they need to one or the other. In other words, you have done the following:
验证不起作用的原因是您正在向读取器添加模式验证,但随后将ValidationEventHandler方法附加到XmlDocument对象。双方XmlDocument并XmlReader能够执行模式验证,他们每个人都有自己XmlSchemaSet并验证事件处理程序,他们用它来进行验证。你已经给了他们每个人所需的一半,而不是他们需要的所有东西。换句话说,您已完成以下操作:
- XmlReader's Schema: SET
- XmlReader's Event Handler: NOT SET
- XmlDocument's Schema: NOT SET
- XmlDocument's Event Handler: SET
- XmlReader 的架构:SET
- XmlReader 的事件处理程序:未设置
- XmlDocument 的架构:未设置
- XmlDocument 的事件处理程序:SET
As such, neither object has all the information it needs to properly validate. The XmlReaderobject will be performing the validation, but you won't be notified of any of the errors that it finds, whereas the XmlDocumentobject will not be doing any validation at all, but does have the capability to notify you, in the event that it did find any validation errors. To fix it, you need to either set the XmlReaderobject's validation event handler, or you need to set the XmlDocumentobject's schema. For instance:
因此,这两个对象都没有正确验证所需的所有信息。该XmlReader对象将执行验证,但您不会收到它发现的任何错误的通知,而该XmlDocument对象根本不会进行任何验证,但确实有能力通知您,以防万一确实发现了任何验证错误。要修复它,您需要设置XmlReader对象的验证事件处理程序,或者您需要设置XmlDocument对象的架构。例如:
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("http://www.w3.org/2001/XMLSchema", schemasFilePath)
settings.ValidationType = ValidationType.Schema
AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf ValidationEventHandler)
Dim reader As XmlReader = XmlReader.Create(xmlFilePath, settings)
' Read the document...
回答by Shivani
It is not calling the Event Handler:
它没有调用事件处理程序:
Protected Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
'Messagebox(e, "error")
Case XmlSeverityType.Warning
'Messagebox(e, "error")
End Select
End Sub

