Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate XML when XML and XSD are both strings

Status
Not open for further replies.

blondebier

Programmer
Jun 19, 2003
142
GB
Hi Guys,

I've written a little class that takes two parameters XMLtoValidate and XSD as strings. See here :

Code:
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema

Public Class ValidateXMLTest

    Dim schemaError As String

    ' Validates XML against XSD - returns error if found
    Public Function ValidateXML(ByVal XMLtoValidate As String, ByVal XSD As String) As String

        Try
            Dim xsdrdr As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(XSD))

            ' Load the schema into the schema object...
            Dim clsSchema As System.Xml.Schema.XmlSchema = System.Xml.Schema.XmlSchema.Read(xsdrdr, Nothing)

            ' Configure the reader to use validation, and add the schema we just loaded...
            Dim clsReaderSettings As New System.Xml.XmlReaderSettings()
            clsReaderSettings.ValidationType = ValidationType.Schema
            clsReaderSettings.Schemas.Add(clsSchema)
            clsReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings

            ' Read the XML into an XmlReader...
            Dim xmlrdr As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(XMLtoValidate))

            ' Create a reader that will read the document and validate it against the XSD...
            Dim clsReader As System.Xml.XmlReader = System.Xml.XmlReader.Create(xmlrdr, clsReaderSettings)

            ' Validate the document...
            Do While clsReader.Read()
            Loop

            clsReader.Close()

            Return ""

        Catch exXml As System.Xml.XmlException
            schemaError = "Error on line " & exXml.LineNumber & ", "
            schemaError += "Position " & exXml.LinePosition & ", "
            schemaError += exXml.Message
            Return schemaError
        Catch exXsd As System.Xml.Schema.XmlSchemaException
            schemaError = "Error on line " & exXsd.LineNumber & ", "
            schemaError += "Position " & exXsd.LinePosition & ", "
            schemaError += exXsd.Message
            Return schemaError
        Catch ex As Exception
            'sqlP.Send("Schema validation exception - " & ex.Message.ToString)
            Return schemaError
        End Try

    End Function

End Class

It doesn't work though... I've lifted some code that I use in another application, but reads the XSD from a file. That one works, this doesn't.

I though it might be an encoding issue... Any ideas?

 
Actually it does work! Grrr! up to a point anyway.

The problem was that I hadn't submitted a namespace in my XMLtoValidate document. It just skipped over this and it appeared to pass. If I put the namespace in all is ok. Schema validation fails.

It would be handy to put in a check to make sure that the namespace on the root element is the same as the XSD.

 
Here is a better version to check the namespace on the XMLtoValidate against the namespace on the XSD.

Code:
Public Class ValidateXMLTest2

    Dim schemaError As String

    ' Validates XML against XSD - returns error if found
    Public Function ValidateXML(ByVal XMLtoValidate As String, ByVal XSD As String) As String

        Try
            ' Check that the XMLtoValidate is well formed
            Dim IsValidXML As New XmlDocument
            Try
                IsValidXML.LoadXml(XMLtoValidate)

            Catch ex As Exception
                Return "XML was not well formed"
            End Try

            Dim xsdReader As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(XSD))

            ' Load the schema into the schema object...
            Dim Schema As System.Xml.Schema.XmlSchema = System.Xml.Schema.XmlSchema.Read(xsdReader, Nothing)

            ' Check that namespace from the schema is the same as the XML to validate
            Dim ns As String = ""
            If Not IsValidXML.FirstChild.Attributes("xmlns") Is Nothing Then
                ns = IsValidXML.FirstChild.Attributes("xmlns").InnerText
            End If

            If Schema.TargetNamespace <> ns Then
                Return "Invalid namespace found"
            End If

            ' Configure the reader to use validation, and add the schema we just loaded...
            Dim SchemaReaderSettings As New System.Xml.XmlReaderSettings()
            SchemaReaderSettings.ValidationType = ValidationType.Schema
            SchemaReaderSettings.Schemas.Add(Schema)
            SchemaReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
            SchemaReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
            SchemaReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessSchemaLocation

            ' Read the XML into an XmlReader...
            Dim xmlReader As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(XMLtoValidate))

            ' Create a reader that will read the document and validate it against the XSD...
            Dim ValidationReader As System.Xml.XmlReader = System.Xml.XmlReader.Create(xmlReader, SchemaReaderSettings)

            ' Validate the document...
            Do While ValidationReader.Read()
            Loop

            ValidationReader.Close()

            Return ""

        Catch exXml As System.Xml.XmlException
            schemaError = "Error on line " & exXml.LineNumber & ", "
            schemaError += "Position " & exXml.LinePosition & ", "
            schemaError += exXml.Message
            Return schemaError
        Catch exXsd As System.Xml.Schema.XmlSchemaException
            schemaError = "Error on line " & exXsd.LineNumber & ", "
            schemaError += "Position " & exXsd.LinePosition & ", "
            schemaError += exXsd.Message
            Return schemaError
        Catch ex As Exception
            'sqlP.Send("Schema validation exception - " & ex.Message.ToString)
            schemaError = ex.Message.ToString
            Return schemaError
        End Try

    End Function

End Class

Feel free to post any other enhancements that would be useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top