简体   繁体   English

如何在VB.net中针对Schema验证XML

[英]How to validate XML against Schema in VB.net

I am trying to validate xml against a schema. 我正在尝试针对模式验证xml。 I am using XmlReaderSetting and trying to follow an example on MSDN but not able to make it work. 我正在使用XmlReaderSetting并尝试在MSDN上关注示例,但无法使其工作。 It doesn't validate the xml even I throw a totally different file against schema. 即使我针对架构抛出一个完全不同的文件,它也不验证xml。 Can anyone explain me what I am missing? 任何人都可以解释我错过了什么吗?

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

You are mixing two different ways to read the XML file. 您正在混合两种不同的方式来读取XML文件。 You are using an XmlReader object and an XmlDocument object. 您正在使用XmlReader对象和XmlDocument对象。 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. 它可以像你一样使用它们,但它确实引入了一些不必要的混淆。

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. 验证无效的原因是您将架构验证添加到阅读器,但随后将ValidationEventHandler方法附加到XmlDocument对象。 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. XmlDocumentXmlReader都能够执行模式验证,它们每个都有自己的XmlSchemaSet和验证事件处理程序,用于执行验证。 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's Schema: SET XmlReader的Schema: SET
  • XmlReader's Event Handler: NOT SET XmlReader的事件处理程序: NOT SET
  • XmlDocument's Schema: NOT SET XmlDocument的架构: NOT SET
  • XmlDocument's Event Handler: SET XmlDocument的事件处理程序: SET

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. XmlReader对象将执行验证,但是您不会收到它发现的任何错误的通知,而XmlDocument对象根本不会进行任何验证,但是有可能在事件中通知您它确实发现了任何验证错误。 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. 要修复它,您需要设置XmlReader对象的验证事件处理程序,或者需要设置XmlDocument对象的架构。 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...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM