简体   繁体   中英

How do I validate a string of XML against an XML Schema file

I'm developing a VB Web Application in .NET3.5 using Visual Studio 2008.

I'm having difficulty in validating some XML as a string before I add it to a HTML form to post to a 3rd party. I have an XML schema file from the 3rd party to validate against and at this point I'd like the application to perform the validation before each post.

After searching I've found references to a XmlValidatingReader but this is obsolete and I'm having difficulty finding another way to do it.

Also all the good examples are in C# - for now I'm stuck with VB. This is what I have so far which I'm looking for help with!

Public Function ValidateXML(ByVal strXML As String) As Boolean

    ' er how do I get the schema file into here?
    Dim schema As XmlReader

    Dim settings As XmlReaderSettings = New XmlReaderSettings()
    settings.Schemas.Add("", schema)
    settings.ValidationType = ValidationType.Schema

    ' When I use LoadXML to get the string I can't use the settings object above to get the schema in??
    Dim document As XmlDocument = New XmlDocument()
    document.LoadXml(strXML)

    document.Validate(AddressOf ValidationEventHandler)

End Function

Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
    ' Gonna return false here but haven't got to it yet! Prob set a variable for use above
End Sub

Thanks

Here's an example: XmlSchemaValidator in VB.NET

UPDATE - Try this:

Public Function ValidateXML(ByVal strXML As String) As Boolean
  Dim xsdPath As String = "path to your xsd"
  Dim schema As XmlReader = XmlReader.Create(xsdPath)
  Dim document As XmlDocument = New XmlDocument()
  document.LoadXml(strXML)
  document.Schemas.Add("", schema)
  document.Validate(AddressOf ValidationEventHandler)
End Function

This is what I ended up going with

Public validationErrors As String = ""

Public Function ValidPortalRequest(ByVal XMLPortalRequest As String) As Boolean
    Try
        Dim objSchemasColl As New System.Xml.Schema.XmlSchemaSet
        objSchemasColl.Add("xxx", "xxx")
        objSchemasColl.Add("xxx", "xxxd")
        Dim xmlDocument As New XmlDocument
        xmlDocument.LoadXml(XMLPortalRequest)
        xmlDocument.Schemas.Add(objSchemasColl)
        xmlDocument.Validate(AddressOf ValidationEventHandler)
        If validationErrors = "" Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Throw
    End Try
End Function

Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
    validationErrors += e.Message & "<br />"
End Sub

Same as Jose's except I've added 2 XSD as a SchemaSet rather than reading them in with an XMLReader.

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