如何在 VB.net 中根据 Schema 验证 XML [英] How to validate XML against Schema in VB.net
问题描述
我正在尝试根据架构验证 xml.我正在使用 XmlReaderSetting 并尝试遵循 MSDN 上的示例,但无法使其工作.即使我针对架构抛出一个完全不同的文件,它也不会验证 xml.谁能解释一下我缺少什么?
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?
谢谢,
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
推荐答案
您正在混合两种不同的方式来读取 XML 文件.您正在使用 XmlReader
对象和 XmlDocument
对象.通常,您只会使用其中一种.两者都可以使用,就像您所做的那样,但它确实会带来一些不必要的混乱.
You are mixing two different ways to read the XML file. You are using an XmlReader
object and an XmlDocument
object. 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.
验证不起作用的原因是您将架构验证添加到读取器,但随后将 ValidationEventHandler
方法附加到 XmlDocument
对象.XmlDocument
和 XmlReader
都能够执行模式验证,它们都有自己的 XmlSchemaSet
和验证事件处理程序,用于执行验证.你已经给了他们每个人所需的一半,而不是他们需要的所有东西.换句话说,您已完成以下操作:
The reason the validation is not working is because you are adding the schema validation to the reader, but then you attach the ValidationEventHandler
method to the XmlDocument
object. Both XmlDocument
and XmlReader
are capable of performing schema validation, and they each have their own XmlSchemaSet
and 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:
- XmlReader 的架构:SET
- XmlReader 的事件处理程序:未设置
- XmlDocument 的架构:未设置
- XmlDocument 的事件处理程序:SET
因此,两个对象都没有正确验证所需的所有信息.XmlReader
对象将执行验证,但您不会收到它发现的任何错误的通知,而 XmlDocument
对象不会在所有,但确实有能力通知您,如果它确实发现了任何验证错误.要修复它,您需要设置 XmlReader
对象的验证事件处理程序,或者您需要设置 XmlDocument
对象的架构.例如:
As such, neither object has all the information it needs to properly validate. The XmlReader
object will be performing the validation, but you won't be notified of any of the errors that it finds, whereas the XmlDocument
object 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 XmlReader
object's validation event handler, or you need to set the XmlDocument
object's schema. For instance:
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...
这篇关于如何在 VB.net 中根据 Schema 验证 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!