473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

validating XML-file against a schema

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
1 4302
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Cees Wesseling | last post by:
Hi, it seems that xmlproc, the default Validating parser, in my setup does not call back to setDocumentLocator. Is there anyway to get a locator in my handler? Below you find an example and its...
4
by: jsonstein | last post by:
I am going nuts trying to find a DTD validation service online... is there any site which offers validation of DTDs online and which does the validation in an authoritative manner? thanks jeffs
30
by: Toni Mcintyre | last post by:
i'm having 2 problems with the http://validator.w3.org 1. if i have: <meta http-equiv="Content-Script-Type" content="text/javascript"> then why do i need <script type=text/javascript>...
2
by: Dale Strickland-Clark | last post by:
A few days ago there was a discussion about which XML parser to use with Python. However, the discussion didn't cover validating parsers, at least, not w3.org XML Schemas. I looked into all the...
4
by: billcoumbe | last post by:
any recommendations? I'm looking for something that will just run from the unix command line to validate large (20-50Mb) XML files against an XML DTD. Ideally something that is actively...
2
by: PapaRandy | last post by:
Hello, I am trying to validate the following .py webpage as HTML (through W3C). I put: ----------------------------------------------------------------------------- print "Content-type:...
1
by: lipsa | last post by:
Dear all, i m v new 2 use DTD.when i directly run my following code,the DTD (i am using internal DTD)is not validating my XML.bt a thirdparty tool is validating. i think some parsing issue. but nt...
4
by: Paul | last post by:
The title says it all. Is there a speed efficient way of validating that a string contains valid Xml without the overhead of try catch around a load attempt? Thanks in advance.
7
by: Thiago Macedo | last post by:
Hi again, folks. i'm developing a windows application that gets data from an Mysql database through PHP server-side scripts. I'm not totally familiar with Requests and XML on .Net 2.0. I'll...
3
by: mathieu | last post by:
Hi there, Could someone please suggest an open source implementation of a validating XML parser ? I am interested in how this thing can (should?) be designed. In particular how the errors should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.