Question Xml validation against a schema

robtyketto

Member
Joined
Jul 23, 2009
Messages
23
Programming Experience
Beginner
Greetings,

I have been following this tutorial which shows how to validate a file against a schema:-
XmlSchemaSet Class (System.Xml.Schema)

My code is below, I hardcoded string values for the xml file and schema file location choosing a file that would specifically error against the schema.

I believe the xml reader settings ValidationEventHandler will only be called IF there is an error, is this correct?

At the moment the xml file is read and the code in the ValidationEventHandler is never triggered.

VB.NET:
Private Sub ValidateXml(ByVal xmlPath As String, ByVal xsdPath As String, ByVal xmlns As String)

        Dim readerSettings As XmlReaderSettings = New XmlReaderSettings()
        readerSettings.Schemas.Add(Nothing, Application.StartupPath & "\quotesschema.xsd")
        readerSettings.ValidationType = ValidationType.Schema
        AddHandler readerSettings.ValidationEventHandler, AddressOf ValidationCallBack

        Dim xmlQuoteFileReader As XmlReader = XmlReader.Create(Application.StartupPath & "\highscore.xml", readerSettings)

        While xmlQuoteFileReader.Read()

        End While

    End Sub

    Private Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs)

        If e.Severity = XmlSeverityType.Warning Then
            Console.Write("WARNING: ")
            Console.WriteLine(e.Message)

        ElseIf e.Severity = XmlSeverityType.Error Then
            Console.Write("ERROR: ")
            Console.WriteLine(e.Message)
        End If

    End Sub

My schema which is valid is below:-

VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="quotefile">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="quote" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="quote">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="date"/>
        <xs:element ref="author"/>
        <xs:element ref="category"/>
        <xs:element ref="text"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="date" type="xs:date" xml:space="default"/>
  <xs:element name="author" type="xs:string"/>
  <xs:element name="category" type="xs:string"/>
  <xs:element name="text" type="xs:string"/>
</xs:schema>

Can anyone please advise why the errors are never written to the console?

I've had a look at other example but I'm obviously not grasping the concept, any help would be appreciated.

Thanks
Rob
 
What is the content of file highscore.xml ?
 
Here's the highscore.xml which should fail against the schema.

VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<highscoretable>

    <highscore>
		<rank>1st</rank>
		<name>Rob</name>
		<score>150</score>
		<scoredatetime>20-09-2009</scoredatetime>
    </highscore>

     <highscore>
		<rank>2nd</rank>
		<name>Tim</name>
		<score>145</score>
		<scoredatetime>23-09-2009</scoredatetime>
    </highscore>

     <highscore>
		<rank>3rd</rank>
		<name>David</name>
		<score>140</score>
		<scoredatetime>02-09-2009</scoredatetime>
    </highscore>

     <highscore>
		<rank>4th</rank>
		<name>Jane</name>
		<score>135</score>
		<scoredatetime>22-09-2009</scoredatetime>
    </highscore>

     <highscore>
		<rank>5th</rank>
		<name>Andrew</name>
		<score>130</score>
		<scoredatetime>11-09-2009</scoredatetime>
    </highscore>

     <highscore>
		<rank>6th</rank>
		<name>Amanda</name>
		<score>125</score>
		<scoredatetime>17-09-2009</scoredatetime>
    </highscore>
    
     <highscore>
 
Yes, it fails, output:
ERROR: The 'highscoretable' element is not declared.
 
Back
Top