XML
  Home arrow XML arrow Page 2 - XML Processing With The XMLReader Object, ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XML Processing With The XMLReader Object, Part 2
By: Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 11
    2004-04-14

    Table of Contents:
  • XML Processing With The XMLReader Object, Part 2
  • Returning to the Library
  • To DTD or Not to DTD
  • Of Nodes and Trees
  • Playing Catch
  • Linking Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    XML Processing With The XMLReader Object, Part 2 - Returning to the Library


    (Page 2 of 6 )

    I'll explain how the XMLValidatingReader works by again referring to the sample XML instance created in the first part of this article. In case you don't remember what it looked like, here it is again:


    <?xml version='1.0'? >
    <library xsi:noNamespaceSchemaLocation="library.xsd" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance >
     <book id="MFRE001">
      <title>XML and PHP</title>
      <author>Vikram Vaswani</author>
      <description>Learn to manage your XML data with PHP</description>
      <price currency="USD">24.95</price>
     </book>
     <book id="MFRE002">
      <title>MySQL - The Complete Reference</title>
      <author>Vikram Vaswani</author>
      <description>Learn everything about this open source database</description>
      <price currency="USD">45.95</price>
      <stock>1000</stock>
     </book>
    </library>

    The only major difference in this version of the XML file is the introduction of the xsi:noNamespaceSchemaLocation attribute. For the uninformed, this holds the location of the Schema against which this document is to be validated.

    And here's the XML Schema against which the XML document listed above was originally built:


    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     
    <xsd:element name="library" type="LibraryType"/>
      
    <xsd:complexType name="LibraryType">
       
    <xsd:sequence maxOccurs="unbounded">
        
    <xsd:element name="book"  type="BookType"/>
       
    </xsd:sequence>
     
    </xsd:complexType>
     
    <xsd:complexType name="BookType">
      
    <xsd:sequence>
       
    <xsd:element name="title" type="xsd:string" />
       
    <xsd:element name="author" type="xsd:string" />
       
    <xsd:element name="description" type="xsd:string" />
       
    <xsd:element name="price">
        
    <xsd:complexType>
         
    <xsd:simpleContent>
          
    <xsd:extension base="xsd:decimal">
           
    <xsd:attribute name="currency" type="xsd:string" />
          
    </xsd:extension>
         
    </xsd:simpleContent>
        
    </xsd:complexType>
       
    </xsd:element>
      
    </xsd:sequence>
      
    <xsd:attribute name="id" type="xsd:string" />
     
    </xsd:complexType>
    </xsd:schema>

    Now for the glue that binds them. Consider the following ASP.NET code, which validates the XML document instance against the XML Schema above:


    <%@ Page Language="C#" Debug="true" %>
    <%@ Import namespace="System.Xml"%>
    <%@ Import namespace="System.Xml.Schema"%>
    <html>
    <head>
    <script runat="server">
     
    Boolean blnValidationSuccess true;
     
    void Page_Load()  {
     
     
    // define variables
     string strXmlFile = http://localhost:2121/xmlpull/library.xml;
     
     // initialize the XML readers  
     XmlTextReader objXmlTxtRdr = new XmlTextReader(strXmlFile);
     XmlValidatingReader objXmlValRdr = new XmlValidatingReader(objXmlTxtRdr);
     
     // set the validation type
     objXmlValRdr.ValidationType = ValidationType.Schema;
     
     // set the validation event handler
     objXmlValRdr.ValidationEventHandler += new ValidationEventHandler
    (ValidationMonitor);
     
     // show some status messages
     output.Text = "Validating file: <b>" + strXmlFile.ToString() + "</b>";
     
     // read XML data
     while (objXmlValRdr.Read()){}
     
     
    output.Text += "<br />Validation <b>" + (blnValidationSuccess == true "successful" "failed") + ".</b>";
      

     objXmlValRdr
    .Close();
     objXmlTxtRdr
    .Close();
     
    }
     
    // display validation errors
    void ValidationMonitor (object sender, ValidationEventArgs args)
    {
       blnValidationSuccess = false;
       output.Text += "<br />Validation Error: <i>" + args.Message + "</i>";
    }
     
    </script>
    </head>
    <body>
    <asp:label id="output" runat="server"/>
    </body>
    </html>

    If you were to test this code using the library.xml file shown above, the XML document instance should pass the validation tests with flying colors:

    Validating file: http://localhost:2121/xmlpull/library.xml
    Validation successful.

    But look what happens if you add a new, unwanted element to the document
    instance:


    <?xml version='1.0'? >
    <library>
     
    <book id="MFRE001">
      
    <title>XML and PHP</title>
      
    <author>Vikram Vaswani</author>
      
    <description>Learn to manage your XML data with PHP</description>
      
    <price currency="USD">24.95</price>
     
    </book>
     
    <book id="MFRE002">
      
    <title>MySQL The Complete Reference</title>
      
    <author>Vikram Vaswani</author>
      
    <description>Learn everything about this open source database</description>
      
    <price currency="USD">45.95</price>
      
    <inventory>12</inventory>
     
    </book>
    </library>

    The XML Schema definition does not allow the XML author to add this new <inventory> element. That's why you'll see the following output when you reload the example in the browser:

    Validating file: http://localhost:2121/xmlpull/library.xml
    Validation Error: The element 'book' has invalid child element 'inventory'.
    An error occurred at http://localhost:2121/xmlpull/library.xml, (15, 4).
    Validation Error: The 'inventory' element is not declared. An error
    occurred at http://localhost:2121/xmlpull/library.xml, (15, 4).
    Validation failed.


    Notice that the error message explicitly highlights the rogue <inventory> element in the XML file.

    Now, let's take a closer look at how this code works. It all starts with the definition of a flag variable to track the validation process.


    <%
    Boolean blnValidationSuccess true;
    %>


    This is followed by the definition of the object required for our example. Here, I need to first initialize a plain-vanilla XmlTextReader object, and then pass this object as a parameter to the new XmlValidatingReader object, as shown below:


    <%
    // initialize the XML readers  
    XmlTextReader objXmlTxtRdr = new XmlTextReader(strXmlFile);
    XmlValidatingReader objXmlValRdr = new XmlValidatingReader(objXmlTxtRdr);
    %>

    Next, I have defined the mechanism to use when validating the XML -- in this case, an XML Schema. This is done via the ValidationType property of the XmlValidatingReader object:


    <%
    // set the validation type
    objXmlValRdr.ValidationType = ValidationType.Schema;
    %>

    You can set the "ValidationType" property of the XmlValidatingReader object to any one of the following:

    ValidationType.None - no validation is required

    ValidationType.Auto - search for a file automatically; if available, carry out validation

    ValidationType.DTD - perform validation using a DTD

    ValidationType.XDR - perform validation using a XDR

    ValidationType.Schema - perform validation using an XML Schema

    While the validator is checking the XML document against the Schema, it generates an event if it encounters an error. Therefore, it's a good idea to define an event handler to trap this event and take appropriate action when it occurs. In this example, I've defined an event handler function named ValidationMonitor(), and associated it with the object via its ValidationEventHandler property:


    <%
     
    // set the validation event handler
    objXmlValRdr.ValidationEventHandler += new ValidationEventHandler
    (ValidationMonitor);
     
    void ValidationMonitor (object senderValidationEventArgs args)
    {
       blnValidationSuccess 
    false;
       output
    .Text += "<br />Validation Error: <i>" args.Message "</i>";
    }
     
    %>


    Notice how the Message property of the object is used to display a user-friendly error message in the browser.

    Finally, assuming no errors in validation, you can iterate over the document and process the XML inside it with the Read() method I showed you in the previous article. Here, the Read() loop is an empty block because I didn't really want to process the data in the file, just validate it to show you how it was done.


    <%
     
    // read XML data
    while (objXmlValRdr.Read()) {
     
    }
     
    output.Text += "<br />Validation <b>" + (blnValidationSuccess == true "successful" "failed") + ".</b>";
     
    %>


    The example closes with a check on the blnValidationSuccess variable, displaying the appropriate outcome of the validation process to the user in the browser.

    More XML Articles
    More By Harish Kamath (c) Melonfire


     

    XML ARTICLES

    - More on Triggers and Styles and Control Temp...
    - Looking at Triggers with Styles and Control ...
    - A Closer Look at Styles and Control Templates
    - Styles and Control Templates
    - Properties and More in XAML
    - Elements and Attributes in XAML
    - XAML in a Nutshell
    - Importing XML Files into Access 2007
    - Using MSXML3.0 with VB 6.0
    - MSXML, concluded
    - MSXML, continued
    - MSXML Tutorial
    - Generating XML Schema Dynamically Using VB.N...
    - XSL Transformations using ASP.NET
    - Applying XSLT to XML Using ASP.NET





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT