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_ValidationEventHandler);
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++;
}
Martin Honnen - 20 Mar 2007 13:55 GMT
> private void ValidateSchema()
> {
[quoted text clipped - 15 lines]
> }
> }
Why do you need all those readers, does
using (XmlReader xmlReader = XmlReader.Create(@"C:\OrderDoc.xml",
xrsXmlReaderSettings))
{
while (xmlReader.Read()) {}
}
not suffice?

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Techno_Dex - 20 Mar 2007 14:41 GMT
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.
.
>> private void ValidateSchema()
>> {
[quoted text clipped - 23 lines]
> }
> not suffice?