.Net Xml schema validation - either it's not working or I'm too tarded to do it

kepler

New member
I can't get this unit test to work. The damned xml validation never picks up that the XML bares no resemblance to the schema.
Code:
    <DeploymentItem("TestAppSample.exe"), TestMethod(), ExpectedException(GetType(System.Xml.XmlException)), Owner("1")> _
    Public Sub Validate_IncorrectXML_ValidSchema()
        Dim target As clsXMLSaver = New clsXMLSaver
        Dim sSet As New System.Xml.Schema.XmlSchemaSet
        Dim strXsdURI As String = GetURIForXSD(CORRECT_XSD)
        Dim strNamespace As String = "http://www.thexxx.com"

        sSet.Add(strNamespace, strXsdURI)
        target.Schemas = sSet
        target.XML = INCORRECT_XML
        target.Validate() ' Re-mark as Friend or re-visit exposure
    End Sub
Xml values:
Code:
    Const INCORRECT_XML As String = "<?xml version=""1.0""?>" & vbCr & _
                                                                    "<person>" & vbCr & _
                                                                      "<name>Bob</name>" & vbCr & _
                                                                      "<address>999 Letsbe Avenue</address>" & vbCr & _
                                                                      "<age>10</age>" & vbCr & _
                                                                    "</person>"

    Const CORRECT_XSD As String = _
    "<?xml version=""1.0""?>" & vbCr & _
     "<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""" & vbCr & _
     "targetNamespace=""http://www.thexxx.com""" & vbCr & _
     "xmlns=""http://www.thexxx.com""" & vbCr & _
     "elementFormDefault=""qualified"" > " & vbCr & _
       "<xs:element name=""book"">" & vbCr & _
         "<xs:complexType>" & vbCr & _
         "<xs:sequence>" & vbCr & _
           "<xs:element name=""title"" type=""xs:string""/>" & vbCr & _
           "<xs:element name=""publisher"" type=""xs:string""/>" & vbCr & _
           "<xs:element name=""publicationYear"" type=""xs:integer""/>" & vbCr & _
           "<xs:element name=""ISBN"" type=""xs:string""/>" & vbCr & _
           "<xs:element name=""noOfCopies"" type=""xs:string""/>" & vbCr & _
         "</xs:sequence>" & vbCr & _
       "</xs:complexType>" & vbCr & _
     "</xs:element>" & vbCr & _
     "</xs:schema>" & vbCr
Class under test:
Code:
Public Class clsXMLSaver

    ' Member object to store the XML
    Private m_xmlDOM As System.Xml.XmlDocument
    Dim eventHandler As System.Xml.Schema.ValidationEventHandler = New System.Xml.Schema.ValidationEventHandler(AddressOf schemaValidateEventHandler)

#Region "QA Demo Code"

    Private Sub schemaValidateEventHandler(ByVal sender As System.Object, ByVal e As System.Xml.Schema.ValidationEventArgs)
        If e.Severity = System.Xml.Schema.XmlSeverityType.Error Then
            Throw e.Exception ' XML Schema Exception
        ElseIf e.Severity = System.Xml.Schema.XmlSeverityType.Warning Then
            Throw e.Exception ' XML Schema Exception
        End If
    End Sub

    Public Property Schemas() As System.Xml.Schema.XmlSchemaSet
        Get
            Return m_xmlDOM.Schemas
        End Get
        Set(ByVal value As System.Xml.Schema.XmlSchemaSet)
            m_xmlDOM.Schemas = value
        End Set
    End Property

    Public Sub Validate()
        m_xmlDOM.Validate(eventHandler)
    End Sub

    Friend Sub Save(ByVal filePath As String)
        m_xmlDOM.Save(filePath)
    End Sub

    Public Function Query(ByVal p_strQuery As String) As System.Xml.XmlNodeList
        Return m_xmlDOM.SelectNodes(p_strQuery)
    End Function

#End Region




    ''' <summary>
    ''' Set the member variable xml object
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        m_xmlDOM = New System.Xml.XmlDocument
        m_xmlSchemas = New System.Xml.Schema.XmlSchemaSet
        AddHandler m_xmlSchemas.ValidationEventHandler, New System.Xml.Schema.ValidationEventHandler(AddressOf schemaValidateEventHandler)
    End Sub

    ''' <summary>
    ''' Kill the objects
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub Finalize()
        m_xmlDOM = Nothing
        MyBase.Finalize()
    End Sub

    ''' <summary>
    ''' Set the XML within the object
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property XML() As String
        Get
            Return m_xmlDOM.OuterXml
        End Get
        Set(ByVal value As String)
            m_xmlDOM.LoadXml(value)
        End Set
    End Property


    ''' <summary>
    ''' This is my summary text
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ReturnUniqueId() As String
        Return "uniqueid"
    End Function

End Class
Does anyone have any ideas? It seems other people have had this problem on the MSDN forums, but with no resolution.
 
Last edited:
I've retried with this method, specified in the MSDN files
Code:
    Public Sub Validate()
        ' Set the validation settings.
        Dim settings As XmlReaderSettings = New XmlReaderSettings()
        settings.ValidationType = ValidationType.Schema
        settings.Schemas = m_xmlDOM.Schemas
        AddHandler settings.ValidationEventHandler, AddressOf schemaValidateEventHandler

        Dim strTempPath As String = System.IO.Path.GetTempFileName & ".xml"
        ' Write the String to file
        My.Computer.FileSystem.WriteAllText(strTempPath, m_xmlDOM.OuterXml, False)
        ' Create the XmlReader object.
        Dim reader As XmlReader = XmlReader.Create(strTempPath, settings)

        ' Parse the file. 
        While reader.Read()
        End While
    End Sub
still refuses to work.
 
Back
Top