简体   繁体   中英

How to validate XML against Schema in VB.net

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?

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. 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.

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's Schema: SET
  • XmlReader's Event Handler: NOT SET
  • XmlDocument's Schema: NOT SET
  • XmlDocument's Event Handler: 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. 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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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