473,385 Members | 1,890 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,385 software developers and data experts.

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 4189
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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,...
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...

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.