473,405 Members | 2,354 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,405 software developers and data experts.

validating XML using XmlValidatingReader - XML ignoring schema

I am having some trouble validating XML using the XmlValidatingReader.
I have created some xml and used the visual studio to generate the
schema. So I am confident that the xml and schema match.

The problem I am having is that the validation event fires for each
node in the xml. It seems to be completely ignoring the schema that I
have used.

I'm wondering if I need to do something extra to tell the xml which
schema to use.

Here's the code. Any help at all would be great.

Thanks
Andy
public sub Main()

Dim sSchema As String = "<?xml version='1.0'?>" & _
"<xs:schema id='MyApp'
targetNamespace='http://tempuri.org/test.xsd'
xmlns:mstns='http://tempuri.org/test.xsd'
xmlns='http://tempuri.org/test.xsd'
xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'
attributeFormDefault='qualified' elementFormDefault='qualified'>" & _
"<xs:element name='MyApp'
msdata:IsDataSet='true' msdata:Locale='en-GB'
msdata:EnforceConstraints='False'>" & _
"<xs:complexType>" & _
"<xs:choice
maxOccurs='unbounded'>" & _
"<xs:element name='os'>" &
_
"<xs:complexType>" & _
"<xs:sequence>" & _
"<xs:element
name='client' minOccurs='0' maxOccurs='unbounded'>" & _
"<xs:complexType>"
& _
"<xs:attribute
name='wmp_v' form='unqualified' type='xs:string' />" & _
"<xs:attribute
name='s_v' form='unqualified' type='xs:string' />" & _
"<xs:attribute
name='file' form='unqualified' type='xs:string' />" & _

"</xs:complexType>" & _
"</xs:element>" & _
"</xs:sequence>" & _
"<xs:attribute
name='v' form='unqualified' type='xs:string' />" & _
"</xs:complexType>" & _
"</xs:element>" & _
"</xs:choice>" & _
"</xs:complexType>" & _
"</xs:element>" & _
"</xs:schema>"

Dim ValidatorReader As New Xml.XmlTextReader(New
StringReader(sSchema))
Dim ValidationSchema As New Xml.Schema.XmlSchema
Dim colSchemas As New Xml.Schema.XmlSchemaCollection

ValidationSchema.Read(ValidatorReader, AddressOf
ValidationEventHandler)
ValidationSchema.Compile(AddressOf ValidationEventHandler)
colSchemas.Add(ValidationSchema)

'XML to validate
Dim sXML As String = "<MyApp
xmlns='http://tempuri.org/test.xsd'>" & _
"<os v='xp'> <client wmp_v='9'
s_v='9' file=''/><client wmp_v='10' s_v='10' file=''/></os>" & _
"</MyApp>"
Dim InputXMLReader As New Xml.XmlTextReader(New
StringReader(sXML))
Dim xmlValidator As New
Xml.XmlValidatingReader(InputXMLReader)

xmlValidator.ValidationType = Xml.ValidationType.Schema
AddHandler xmlValidator.ValidationEventHandler, AddressOf
ValidationEventHandler

'Add schema to the validating reader
xmlValidator.Schemas.Add(colSchemas)

While xmlValidator.Read()
'Validation even is fired for evey node.
End While

Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try

End Sub

Public Shared Sub ValidationEventHandler(ByVal sender As Object,
ByVal e As Xml.Schema.ValidationEventArgs)
Select Case e.Severity
Case Xml.Schema.XmlSeverityType.Error
Debug.WriteLine(e.Exception.Message)
Case Xml.Schema.XmlSeverityType.Warning
Debug.WriteLine(e.Exception.Message)
End Select

End Sub
Nov 12 '05 #1
1 2938
"Andy" <an********@hotmail.com> wrote in message news:6a*************************@posting.google.co m...
I am having some trouble validating XML using the XmlValidatingReader. : : The problem I am having is that the validation event fires for each
node in the xml. It seems to be completely ignoring the schema that I
have used.
Is what you're seeing a number of "element is not declared" errors?
I'm wondering if I need to do something extra to tell the xml which
schema to use.
Well, let's look carefully at how ValidationSchema is being created.

: : Dim ValidationSchema As New Xml.Schema.XmlSchema
ValidationSchema.Read(ValidatorReader, AddressOf
ValidationEventHandler)
ValidationSchema.Compile(AddressOf ValidationEventHandler)
colSchemas.Add(ValidationSchema)


On the first line, ValidationSchema is created as an empty schema.

On the second line, XmlSchema::Read( ) is being called to load in the
XML Schema Document contained in sSchema and _then throwing
it all away_.

On the third line, Compile( ) is happily compiling the empty schema
you created in the first line.

On the fourth line, Add( ) adds the empty schema to the collection of
schemas you later use to validate the the instance document. Of
course, since it is empty, every element encountered in the instance
document receives an "element is not declared" error.

Being an observant programmer, I'm sure your eyes bugged out when
I described what was happening on the second line, so let's go back
to that. :-)

I say "throwing it all away" because Read is a Shared Sub. Now, in
some programming languages (like C#) this is a very difficult typo to
make, because the compiler will emit an error should you call such a
static method using an instance object. However, Visual Basic is
much more forgiving, and will just assume, since ValidationSchema
IS an XmlSchema anyway, that you really wanted to call XmlSchema::
Read( ) and didn't want a diagnostic.

So, whereas from looking at the line,

ValidationSchema.Read( ValidatorReader, AddressOf veh)

you might think [where are the Cars?] the Read operation is feeding
its resulting SOM into the ValidationSchema object that you're going
to compile and add into a collection later, what is actually happening
is,

XmlSchema.Read( ValidatorReader, AddressOf veh)

XmlSchema::Read( ) returns an XmlSchema. There is no variable on
the left-hand side to receive that XmlSchema that it's read, so it just
gets thrown away.

The answer then, is to capture the XmlSchema returned by Read( ):

Dim ValidationSchema As Xml.Schema.XmlSchema ' New isn't required.
ValidationSchema = XmlSchema.Read( _
ValidatorReader, _
AddressOf ValidationEventHandler _
)
' ValidationSchema is no longer empty.
Derek Harmon
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Adam Child | last post by:
Hi All, I'm trying to validate an xml document. I'm having trouble setting the default namespace of the xml document. If I hard encode the namespace in the xml file then everything works fine....
1
by: Ryan | last post by:
I have a very complex XDR schema that uses namespaces: xmlns="urn:schemas-microsoft-com:xml-data" xmlns:b="urn:schemas-microsoft-com:BizTalkServer" xmlns:d="urn:schemas-microsoft-com:datatypes"...
6
by: Robert Reineri | last post by:
Hello, New to the XML world and .NET. I have what I believe to be a simple problem, but I have read the .NET docs till I'm blue in the face and still can't locate a simple example of how to...
4
by: Tomas Rivas | last post by:
I am trying to validate an xml file and schema and when I am trying to validate I am getting the following error. I have been trying to come out with a solution but I have failed so far. The...
2
by: Joris Janssens | last post by:
I'm trying to write a program for validating XHTML 1.1-documents against the XHTML 1.1 DTD (which is actually the same as validating an XML-file) but I always get a "(404) Not found" error. This...
1
by: Brendon | last post by:
I have 2 Xsd's The one contains basic type definitions <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
by: Mike Bridge | last post by:
Hi- I've created an XHTML extension module which validates correctly using the W3C online schema validator, but fails when I use the .net 1.1 validator. It seems to be choking on an included W3C...
2
by: josh | last post by:
Hi, I am trying to validate cXML documents against cXML.dtd using the XmlValidatingReader. If I set the XMLValidatingReader's ValidatingType to ValidationType.DTD, I get the following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.