By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,197 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,197 IT Pros & Developers. It's quick & easy.

validating XML-file against a schema

P: n/a
Hi,

I load an Xml-file "customers.xml" into a DataSet (works fine) but then how
do I validate it against a schema (e.g. customers.xsd) ?

my customers.xml:
<?xml version="1.0" encoding="utf-8"?>|
<customers xmlns="http://tempuri.org/customers.xsd">
<Customer ID="1000">
<FirstName>Greg</FirstName>
<LastName>Chapman</LastName>
</Customer>
<Customer ID="1001">
<FirstName>Sean</FirstName>
<LastName>Purcell</LastName>
</Customer>
</customers>

VB Sample code : first part : loading Xml-file into a DataSet
' Create a new DataSet.
Dim newDataSet As New DataSet("New DataSet")
Dim xmlFilename As String = "customers.xml"

' Create new FileStream to read schema with.
Dim fsReadXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Open)
' Create an XmlTextReader to read the file.
Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)

' Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader)
myXmlReader.Close()
Me.DataGrid1.DataSource = newDataSet.Tables(0)

('till here works fine : the data is nicely shown in the grid)

Second part : how do I validate the data in the dataSet against a schema ??
to test it do I change a type definition in the schema-file from string to
short for the FirstName-field

part of customers.xsd:
<xs:sequence>
<xs:element name="FirstName" type="xs:short" minOccurs="0"
msdata:Ordinal="0" />
<xs:element name="LastName" type="xs:string" minOccurs="0"
msdata:Ordinal="1" />
</xs:sequence>

validating the XML-file directly in VisualStudio gives me an expected
validation-error (OK)
..... but validating it in VB as follows :

' Read the XML document into the DataSet.
newDataSet.ReadXmlSchema("customers.xsd")
newDataSet.ReadXml(myXmlReader, XmlReadMode.ReadSchema)
Me.DataGrid1.DataSource = newDataSet.Tables(0)

I expect a run-time error or something but NO, the data is still shown in
the grid ??
so how do I validate in VB ?

thanks for any help

Chris
Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Christian wrote:
' Read the XML document into the DataSet.
newDataSet.ReadXmlSchema("customers.xsd")
newDataSet.ReadXml(myXmlReader, XmlReadMode.ReadSchema)
Me.DataGrid1.DataSource = newDataSet.Tables(0)

I expect a run-time error or something but NO, the data is still shown in
the grid ??
so how do I validate in VB ?


Use XmlValidatingReader. See "Validation of XML with
XmlValidatingReader" at
http://msdn.microsoft.com/library/de...tingReader.asp

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.