Compare XML with XSD file

STEVESKI07

Member
Joined
Sep 12, 2007
Messages
17
Programming Experience
1-3
I'm taking values from a dataset and putting them into an XML file. I have a XSD schema file that I want to validate the XML file with to make sure all the fields are valid. Does anybody have some sample code of how I compare an xml file with a xsd schema file?

Thanks in advance.
 
VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles MyBase.Load

		ValidateXml("C:\Xml\HeadCount.xml", "C:\Xml\HeadCount.xsd", "xsdHeadCount")

	End Sub

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

		Dim schemaSet As New XmlSchemaSet()
		schemaSet.Add(xmlns, xsdPath)

		Dim readerSettings As New XmlReaderSettings()
		readerSettings.ValidationType = ValidationType.Schema
		readerSettings.Schemas = schemaSet

		AddHandler readerSettings.ValidationEventHandler, AddressOf ValidationCallBack

		Dim xmlReader As XmlReader = xmlReader.Create(xmlPath, readerSettings)

		While xmlReader.Read()
		End While

	End Sub

VB.NET:
	Private Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs)
		Debug.WriteLine("Validation failure: {0}", e.Message)
	End Sub

Got these sample failed validation files from Validation Using an Inline XML Schema with XmlReader and modified them to be in seperate files rather than inline.

HeadCount.xml
VB.NET:
<?xml version='1.0'?>
<hc:HeadCount xmlns:hc='xsdHeadCount'>
    <ID>12365</ID>
    <ID>43295</ID>
    <division>Accounting</division>
</hc:HeadCount>

HeadCount.xsd
VB.NET:
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
            xmlns='xsdHeadCount'
            targetNamespace='xsdHeadCount'>
    <xs:element name='HeadCount'>
        <xs:complexType>
            <xs:sequence>
                <xs:element name='ID' type='xs:unsignedShort' maxOccurs='unbounded' />
            </xs:sequence>
            <xs:attribute name='division' type='xs:string' use='optional' default='QA'/>
        </xs:complexType>
    </xs:element>
</xs:schema>

Immediate window shows:

The element 'HeadCount' in namespace 'xsdHeadCount' has invalid child element 'division'. List of possible elements expected: 'ID'.: Validation failure: {0}
 
Matt, thanks again for all the help. I have another quick question. I'm writing the XML document from a dataset. I am currently writing to a file, but I would just like to write to a xml variable and compare that variable against the external schema file. Is there any way of doing this? I'm having a real hard time trying to write the xml internally without actually having to save the file and then open it up again.

Thanks!
 
One way is to write the dataset to a MemoryStream and create the reader from this:
VB.NET:
Dim mem As New IO.MemoryStream
theDataset.WriteXml(mem)
mem.Position = 0
Dim xmlReader As XmlReader = xmlReader.Create(mem, readerSettings)
 
Thank you! That worked just how I wanted it to. One more quick question and then everything is working 100% (I hope). This is the xml that the dataset is generating. The <Table> tags are throwing off the schema comparison. Is there anyway to tell the dataset to not print those tags when the xml is being written?

VB.NET:
<Event>
  <Table>
    <StartDate>2004-12-11T07:19:33-05:00</StartDate>
    <EndDate>2005-08-23T18:33:33.78-04:00</EndDate>
    <Name>EventTest1</Name>
    <Location>VA</Location>
    <EventType>1</EventType>
    <CreatedBy>TestUser</CreatedBy>
    <CreatedDate>2004-12-11T07:19:33-05:00</CreatedDate>
    <OnlineOffline>false</OnlineOffline>
    <Uploaded>false</Uploaded>
  </Table>
</Event>
 
Back
Top