473,405 Members | 2,338 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.

XmlReaderSettings ValidationEventHandler Firing Twice

I have an issue with the EventHandler for the XmlReaderSettings object being
called twice during validation and I'm a little stumpped as to why. The
error message being given is the same error message both times. This is an
intentional error that I have introduced into the XML file to test my
validation code so the second time it occurs is definately invalid and
should not occur (I have successfully tested the valid version of the XML
file). My XML file has references to about 3 other namespaces. My XSD that
I am using references about 4 other namespaces and redefines one of the
namespaces for half of it's use. I'm wondering if this redefinition and the
way the schema is written is causing the XmlReaderSetting object to find the
namespace twice and thus fires the event twice, but I can't confirm this.
The reason I think this is the problem is because when I step through the
Event Handler code (i.e. F11) the control never leaves the method, rather it
steps back to the top of the EventHandler like the event was raised a second
time. I have checked my code and the EventHandler is only being assigned
once. It's not being assigned behind the scenes either, as when I comment
out the EventHandler assignment I get the expected
XmlSchemaValidationException which is the less graceful exit path. Has
someone seen this before or can confirm that a schema namespace being
declared twice in an XSD would cause this type of problem in an
XmlReaderSettings object? I have encluded some sample examples of both XSD
and code used to validate the XML file. Any help would be appriciated.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:abc="http://www.ABC.org/schemas"
xmlns:abc-tp="http://www.ABC.org/schemas/Types"
xmlns:order="http://www.ACME.com/schemas"
targetNamespace="http://www.ACME.com/schemas" elementFormDefault="qualified"
attributeFormDefault="unqualified">

<xsd:import namespace="http://www.ABC.org/schemas"
schemaLocation="ABC.xsd"/>
<xsd:import namespace="http://www.ABC.org/schemas/Types"
schemaLocation="ABC_Types.xsd"/>

<xsd:redefine schemaLocation="Header.xsd">
<xsd:simpleType name="schemaNameString">
<xsd:restriction base="order:schemaNameString">
<xsd:enumeration value="ACMEOrder"/>
</xsd:restriction>
</xsd:simpleType>
...... <!-- More ReDefinitions Here -->
</xsd:redefine>

<!-- Start using the targetNamespace here as opposed to the ACMEOrder
reference -->
<!-- to the same namespace up above in the ReDefinition section -->
<xsd:element name="Order"></xsd:element>
<xsd:element name="StartDate" type="xsd:date"></xsd:element>
<xsd:element name="EndDate" type="xsd:date"></xsd:element>
</xsd:schema>

private void ValidateSchema()
{
xssXmlSchemaSet = new XmlSchemaSet();
xrsXmlReaderSettings = new XmlReaderSettings();

xssXmlSchemaSet.Add("http://www.ACME.com/schemas", @"C:\Order.xsd");
xrsXmlReaderSettings.ValidationType = ValidationType.Schema;
xrsXmlReaderSettings.ValidationEventHandler += new
ValidationEventHandler(xrsXmlReaderSettings_Valida tionEventHandler);
xrsXmlReaderSettings.Schemas = xssXmlSchemaSet;

xtrXmlTextReader = new XmlTextReader(@"C:\OrderDoc.xml");
xrReader = XmlReader.Create(xtrXmlTextReader, xrsXmlReaderSettings);
using (XmlReader xrXmlReader = XmlReader.Create(xrReader,
xrsXmlReaderSettings))
{
while (xrXmlReader.Read()) ;
}
}

private void xrsXmlReaderSettings_ValidationEventHandler(object sender,
ValidationEventArgs e)
{
msErrorMessage = msErrorMessage + e.Message + "\r\n";
miErrorsCount++;
}


Mar 19 '07 #1
2 5263
Techno_Dex wrote:
private void ValidateSchema()
{
xssXmlSchemaSet = new XmlSchemaSet();
xrsXmlReaderSettings = new XmlReaderSettings();

xssXmlSchemaSet.Add("http://www.ACME.com/schemas", @"C:\Order.xsd");
xrsXmlReaderSettings.ValidationType = ValidationType.Schema;
xrsXmlReaderSettings.ValidationEventHandler += new
ValidationEventHandler(xrsXmlReaderSettings_Valida tionEventHandler);
xrsXmlReaderSettings.Schemas = xssXmlSchemaSet;

xtrXmlTextReader = new XmlTextReader(@"C:\OrderDoc.xml");
xrReader = XmlReader.Create(xtrXmlTextReader, xrsXmlReaderSettings);
using (XmlReader xrXmlReader = XmlReader.Create(xrReader,
xrsXmlReaderSettings))
{
while (xrXmlReader.Read()) ;
}
}
Why do you need all those readers, does
using (XmlReader xmlReader = XmlReader.Create(@"C:\OrderDoc.xml",
xrsXmlReaderSettings))
{
while (xmlReader.Read()) {}
}
not suffice?


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 20 '07 #2
Much obliged. In my attempt to learn and piece together the new v2.0 Xml
parsing and validation functionality I merged code from two different
sources and in the process completely blew past the fact that I was using
two XmlReaders (one instantiated from the other) tied to the same
XmlReaderSettings object. It makes complete logical sense now as to why I
was receiving the event twice.
..
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Techno_Dex wrote:
>private void ValidateSchema()
{
xssXmlSchemaSet = new XmlSchemaSet();
xrsXmlReaderSettings = new XmlReaderSettings();

xssXmlSchemaSet.Add("http://www.ACME.com/schemas", @"C:\Order.xsd");
xrsXmlReaderSettings.ValidationType = ValidationType.Schema;
xrsXmlReaderSettings.ValidationEventHandler += new
ValidationEventHandler(xrsXmlReaderSettings_Valid ationEventHandler);
xrsXmlReaderSettings.Schemas = xssXmlSchemaSet;

xtrXmlTextReader = new XmlTextReader(@"C:\OrderDoc.xml");
xrReader = XmlReader.Create(xtrXmlTextReader, xrsXmlReaderSettings);
using (XmlReader xrXmlReader = XmlReader.Create(xrReader,
xrsXmlReaderSettings))
{
while (xrXmlReader.Read()) ;
}
}

Why do you need all those readers, does
using (XmlReader xmlReader = XmlReader.Create(@"C:\OrderDoc.xml",
xrsXmlReaderSettings))
{
while (xmlReader.Read()) {}
}
not suffice?


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Mar 20 '07 #3

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

Similar topics

2
by: pawel.pabich | last post by:
Hajo, I want to validate xml against DTD but I am not able to load DTD schema. My code looks like that: XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false;...
2
by: alien2_51 | last post by:
Can some one tell me why the onclick is firing twice for the radion button and checkbox controls...? <%@ Page Language="vb" AutoEventWireup="false" Codebehind="Testing.aspx.vb"...
4
by: Larry Morris | last post by:
The following code, pasted into a web form with a link button on it, will cause the page_unload event to fire twice. If I remove the response.redirect, the problem goes away :). I've got a work...
4
by: Seraph | last post by:
Again, I'm rather new here, so if I fail to follow any etiquette, please forgive me and let me know what I've done wrong, but I think this might interest quite a few people. One of my colleaques...
28
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when...
4
by: Brian P | last post by:
I have a service that is driven by a timer that fires every 5 seconds. For the most part, it works fine. But every once in a while the timer fires twice. In the log I can see that when it fires...
0
by: Jen | last post by:
My main question: "How can I get a TextReader or Stream object from an existing XmlReader?". Read on for more: I have an existing XmlReader. Let's call it reader1. I'm creating another reader,...
4
by: ShaneFowlkes | last post by:
I have a odd thing happening. I have a sub that is called upon a button click and it seems to be firing TWICE. I have no idea how this is happening but I suspect is has something to do with...
14
by: TS | last post by:
I have this custom data list control and i override the onItemDatabound event. After upgrading to vs 2005, this event is not always getting called, though it does at other times. No changes were...
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
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
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
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.