472,950 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 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 4277
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.