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.
My schema which is valid is below:-
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
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