Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2007

Tip: Looking for answers? Try searching our database.

XmlReaderSettings ValidationEventHandler Firing Twice

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Techno_Dex - 19 Mar 2007 22:19 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.