473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XML Validating Reader and XmlSchemaException

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 of the post. I am
using VS.net 2003, .Net 1.1, Win2k Server

I have a simple schema and a simple XML document.

The problem is with how I handle errors from the XmlValidatingReader, for
this, there are two options:

Option 1: set the ValidationEventHandler to a call back routine.
This works without an issue, if my XML document has two invalid element
values, the callback is called twice and the proper errors are reported.
No problems.

Output using option 1 with the callback:

Read: 'XmlDeclaration'. Name = 'xml', Value='version="1.0"
encoding="utf-8"'
Read: 'Element'. Name = 'Root', Value=''
Read: 'Element'. Name = 'Record', Value=''
Read: 'Element'. Name = 'TheElementInt', Value=''
Read: 'Text'. Name = '', Value='1ss3'
The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has an
invalid value according to its data type. An error occurred at , (4, 24).
Read: 'EndElement'. Name = 'TheElementInt', Value=''
Read: 'Element'. Name = 'TheElementString', Value=''
Read: 'Text'. Name = '', Value='abcdezxcvbasdasd'
The 'http://tempuri.org/TheSchema.xsd:TheElementString' element has an
invalid value according to its data type. An error occurred at , (5, 39).
Read: 'EndElement'. Name = 'TheElementString', Value=''
Read: 'EndElement'. Name = 'Record', Value=''
Read: 'EndElement'. Name = 'Root', Value=''
Option 2: Don't set the ValidationEventHandler. (I commented it out in
my sample)
By doing this the Read() method will toss a XmlSchemaException exception
in the event that a validation against the schema has failed. The
problem I seem to be encountering on this is it (the validating reader)
keeps throwing an exception referencing the first error every time I read
an element/endelement nodetype. I don't believe this is correct, is it?

Here is the output of my program for option 2. The first error
encountered is valid, 1ss3 is not an integer. However the error about my
string being to long is not reported. The remaining errors only ever
refer to the offending "TheElementInt" element.

Read: 'XmlDeclaration'. Name = 'xml', Value='version="1.0"
encoding="utf-8"'
Read: 'Element'. Name = 'Root', Value=''
Read: 'Element'. Name = 'Record', Value=''
Read: 'Element'. Name = 'TheElementInt', Value=''
Read: 'Text'. Name = '', Value='1ss3'
ERROR-->EndElement - TheElementInt:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (4,
24).
ERROR-->Element - TheElementString:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (5,
4).
Read: 'Text'. Name = '', Value='abcdezxcvbasdasd'
ERROR-->EndElement - TheElementString:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (5,
39).
ERROR-->EndElement - Record:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (6,
4).
ERROR-->EndElement - Root:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (7,
3).
ERROR-->None - :
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (7,
8).
Why the difference in how the two items function?

Thanks in advance..

Craig B.

Here is the schema I am using.

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://tempuri.org/TheSchema.xsd"
elementFormDefault="qualified"
targetNamespace="http://tempuri.org/TheSchema.xsd" id="TheSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" name="Record">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1"
name="TheElementInt" type="xs:int" />
<xs:element minOccurs="1" maxOccurs="1"
name="TheElementString">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Here is my sample XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns="http://tempuri.org/TheSchema.xsd">
<Record>
<TheElementInt>1ss3</TheElementInt>
<TheElementString>abcdezxcvbasdasd</TheElementString>
</Record>
</Root>

Here is the code I am using to validate (just throw it in a WindowsForms
project) There are no class level variables required.:

// Load the schema and return an instance of the XmlSchema Class
// to be added to the validating reader.
private System.Xml.Schema.XmlSchema GetTheSchema()
{
System.IO.FileStream fs;
System.Xml.Schema.XmlSchema xsd;

fs = new System.IO.FileStream(@"m:\Devcode.Net\ValidatorTes t
\TheSchema.xsd", System.IO.FileMode.Open);
xsd = System.Xml.Schema.XmlSchema.Read(fs, null);
fs.Close();
xsd.Compile(null);
return xsd;
}

// the guts of the matter
private void button1_Click(object sender, System.EventArgs e)
{
System.IO.FileStream fs = new System.IO.FileStream(@"m:\Devcode.Net
\ValidatorTest\Sample.xml", System.IO.FileMode.Open);
System.Xml.XmlTextReader xmlr = new System.Xml.XmlTextReader (fs);
System.Xml.XmlValidatingReader xvr = new
System.Xml.XmlValidatingReader(xmlr);

System.Xml.Schema.XmlSchema theSchema;
theSchema = GetTheSchema();

xvr.Schemas.Add (theSchema);
//xvr.ValidationEventHandler +=new
System.Xml.Schema.ValidationEventHandler(xvr_Valid ationEventHandler);
while (xvr.EOF == false)
{
try
{
xvr.Read();
switch (xvr.NodeType)
{
case System.Xml.XmlNodeType.Document:
case System.Xml.XmlNodeType.Element:
case System.Xml.XmlNodeType.EndElement:
case System.Xml.XmlNodeType.Text:
case System.Xml.XmlNodeType.XmlDeclaration:
case System.Xml.XmlNodeType.Entity:
case System.Xml.XmlNodeType.EntityReference:
case System.Xml.XmlNodeType.EndEntity:
case System.Xml.XmlNodeType.Comment:
case System.Xml.XmlNodeType.CDATA:
case System.Xml.XmlNodeType.Attribute:
System.Diagnostics.Debug.WriteLine
(string.Format ("Read: '{0}'. Name = '{1}', Value='{2}'", xvr.NodeType,
xvr.Name, xvr.Value));
break;
}
}
catch (System.Xml.Schema.XmlSchemaException xsex)
{
System.Diagnostics.Debug.WriteLine (string.Format
("ERROR-->{0} - {1}:{2}", xvr.NodeType, xvr.Name, xvr.Value));
System.Diagnostics.Debug.WriteLine ("ERROR-->" +
xsex.Message);
}
}
xvr.Close();
xmlr.Close();
}

private void xvr_ValidationEventHandler(object sender,
System.Xml.Schema.ValidationEventArgs e)
{
System.Diagnostics.Debug.WriteLine (e.Message);
}
Nov 12 '05 #1
1 4198
In case where you did not hook up the event handler, the reader throws an
Exception after which the reader can not be used for further validation, if
you want all errors to be reported, always attach an event handler.

"Craig Beuker" <be**********@shaw.ca> wrote in message
news:Xn******************************@207.46.248.1 6...
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 of the post. I am
using VS.net 2003, .Net 1.1, Win2k Server

I have a simple schema and a simple XML document.

The problem is with how I handle errors from the XmlValidatingReader, for
this, there are two options:

Option 1: set the ValidationEventHandler to a call back routine.
This works without an issue, if my XML document has two invalid element
values, the callback is called twice and the proper errors are reported.
No problems.

Output using option 1 with the callback:

Read: 'XmlDeclaration'. Name = 'xml', Value='version="1.0"
encoding="utf-8"'
Read: 'Element'. Name = 'Root', Value=''
Read: 'Element'. Name = 'Record', Value=''
Read: 'Element'. Name = 'TheElementInt', Value=''
Read: 'Text'. Name = '', Value='1ss3'
The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has an
invalid value according to its data type. An error occurred at , (4, 24).
Read: 'EndElement'. Name = 'TheElementInt', Value=''
Read: 'Element'. Name = 'TheElementString', Value=''
Read: 'Text'. Name = '', Value='abcdezxcvbasdasd'
The 'http://tempuri.org/TheSchema.xsd:TheElementString' element has an
invalid value according to its data type. An error occurred at , (5, 39).
Read: 'EndElement'. Name = 'TheElementString', Value=''
Read: 'EndElement'. Name = 'Record', Value=''
Read: 'EndElement'. Name = 'Root', Value=''
Option 2: Don't set the ValidationEventHandler. (I commented it out in
my sample)
By doing this the Read() method will toss a XmlSchemaException exception
in the event that a validation against the schema has failed. The
problem I seem to be encountering on this is it (the validating reader)
keeps throwing an exception referencing the first error every time I read
an element/endelement nodetype. I don't believe this is correct, is it?

Here is the output of my program for option 2. The first error
encountered is valid, 1ss3 is not an integer. However the error about my
string being to long is not reported. The remaining errors only ever
refer to the offending "TheElementInt" element.

Read: 'XmlDeclaration'. Name = 'xml', Value='version="1.0"
encoding="utf-8"'
Read: 'Element'. Name = 'Root', Value=''
Read: 'Element'. Name = 'Record', Value=''
Read: 'Element'. Name = 'TheElementInt', Value=''
Read: 'Text'. Name = '', Value='1ss3'
ERROR-->EndElement - TheElementInt:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (4,
24).
ERROR-->Element - TheElementString:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (5,
4).
Read: 'Text'. Name = '', Value='abcdezxcvbasdasd'
ERROR-->EndElement - TheElementString:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (5,
39).
ERROR-->EndElement - Record:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (6,
4).
ERROR-->EndElement - Root:
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (7,
3).
ERROR-->None - :
ERROR-->The 'http://tempuri.org/TheSchema.xsd:TheElementInt' element has
an invalid value according to its data type. An error occurred at , (7,
8).
Why the difference in how the two items function?

Thanks in advance..

Craig B.

Here is the schema I am using.

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://tempuri.org/TheSchema.xsd"
elementFormDefault="qualified"
targetNamespace="http://tempuri.org/TheSchema.xsd" id="TheSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" name="Record">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1"
name="TheElementInt" type="xs:int" />
<xs:element minOccurs="1" maxOccurs="1"
name="TheElementString">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Here is my sample XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns="http://tempuri.org/TheSchema.xsd">
<Record>
<TheElementInt>1ss3</TheElementInt>
<TheElementString>abcdezxcvbasdasd</TheElementString>
</Record>
</Root>

Here is the code I am using to validate (just throw it in a WindowsForms
project) There are no class level variables required.:

// Load the schema and return an instance of the XmlSchema Class
// to be added to the validating reader.
private System.Xml.Schema.XmlSchema GetTheSchema()
{
System.IO.FileStream fs;
System.Xml.Schema.XmlSchema xsd;

fs = new System.IO.FileStream(@"m:\Devcode.Net\ValidatorTes t
\TheSchema.xsd", System.IO.FileMode.Open);
xsd = System.Xml.Schema.XmlSchema.Read(fs, null);
fs.Close();
xsd.Compile(null);
return xsd;
}

// the guts of the matter
private void button1_Click(object sender, System.EventArgs e)
{
System.IO.FileStream fs = new System.IO.FileStream(@"m:\Devcode.Net
\ValidatorTest\Sample.xml", System.IO.FileMode.Open);
System.Xml.XmlTextReader xmlr = new System.Xml.XmlTextReader (fs);
System.Xml.XmlValidatingReader xvr = new
System.Xml.XmlValidatingReader(xmlr);

System.Xml.Schema.XmlSchema theSchema;
theSchema = GetTheSchema();

xvr.Schemas.Add (theSchema);
//xvr.ValidationEventHandler +=new
System.Xml.Schema.ValidationEventHandler(xvr_Valid ationEventHandler);
while (xvr.EOF == false)
{
try
{
xvr.Read();
switch (xvr.NodeType)
{
case System.Xml.XmlNodeType.Document:
case System.Xml.XmlNodeType.Element:
case System.Xml.XmlNodeType.EndElement:
case System.Xml.XmlNodeType.Text:
case System.Xml.XmlNodeType.XmlDeclaration:
case System.Xml.XmlNodeType.Entity:
case System.Xml.XmlNodeType.EntityReference:
case System.Xml.XmlNodeType.EndEntity:
case System.Xml.XmlNodeType.Comment:
case System.Xml.XmlNodeType.CDATA:
case System.Xml.XmlNodeType.Attribute:
System.Diagnostics.Debug.WriteLine
(string.Format ("Read: '{0}'. Name = '{1}', Value='{2}'", xvr.NodeType,
xvr.Name, xvr.Value));
break;
}
}
catch (System.Xml.Schema.XmlSchemaException xsex)
{
System.Diagnostics.Debug.WriteLine (string.Format
("ERROR-->{0} - {1}:{2}", xvr.NodeType, xvr.Name, xvr.Value));
System.Diagnostics.Debug.WriteLine ("ERROR-->" +
xsex.Message);
}
}
xvr.Close();
xmlr.Close();
}

private void xvr_ValidationEventHandler(object sender,
System.Xml.Schema.ValidationEventArgs e)
{
System.Diagnostics.Debug.WriteLine (e.Message);
}

Nov 12 '05 #2

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

Similar topics

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...
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...
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...
7
by: SparkPlug | last post by:
Does anyone know why I might be getting an XmlSchemaException in App.config configured as below to use the UIP Application Block? I get the following error for every element, sub-element and...
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...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
0
by: bg | last post by:
hi all, I need to validate, via code, some resx files. I thought I could do something like this: bool IsValidateResx(string input) { XmlTextReader tr = new XmlTextReader(input);...
6
by: Andy B | last post by:
I need to make sure that a file saved in a particular place is a valid xml file that fits a certain schema. Where would I get started doing this? The original file would have been created and saved...
3
by: jh3an | last post by:
Please give me your advice! I made two files according to xml book, but when validating these two files, it gives me an error that I totally don't understand. Is there a problem in these...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.