views:

1383

answers:

2

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

+1  A: 

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
Jose Basilio
Hi Jose, that was a useful example but I had seen it before. I think that code is using a built in schema for the XML / or schema link as it doesn't show how the schema is added, is that right? Also it still uses the obsolete XmlValidatingReader
David A Gibson
Give the code above a try. Hopefully it works.
Jose Basilio
I came up with the same thing so I'll accept your answer, cheers
David A Gibson
+1  A: 

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.

David A Gibson