XML
  Home arrow XML arrow Page 3 - 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 - To DTD or Not to DTD


    (Page 3 of 6 )

    Legacy is bitter reality and so, while XML Schemas are the way forward as far as validation is concerned, don't be surprised when you come across a DTD or two in the XML framework that you are using. In such situations, you'll also need to know how you can use a DTD to validate an XML document instance.

    Here's the updated XML file -- notice it now includes a reference to a DTD instead of an XML Schema:


    <?xml version='1.0'? >
    <!DOCTYPE library SYSTEM "library.dtd">
    <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>
     
    </book>
    </library>

    This brings us to the actual beast -- the library.dtd DTD file:


    <!ELEMENT library (book+)>
    <!ELEMENT book (title,author,description,price)>
    <!ATTLIST book id CDATA #REQUIRED>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT author (#PCDATA)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT description (#PCDATA)>
    <!ELEMENT price (#PCDATA)>
    <!ATTLIST price currency CDATA #REQUIRED>

    A close look at this file and you will see that it describes the structure of the XML document instance fairly well. Of course, in between all the element and attributes are quaint symbols and keywords that will make sense only to DTD experts (if you don't belong to that elite group, you can start with the reference links provided at the end of this article).

    And to complete this jigsaw, we have the ASP.NET code that uses the XmlValidatingReader object to tst the XML document instance against the DTD, as shown below:


    <%@ 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  
     // and set the ValidationType
     XmlTextReader objXmlTxtRdr = new XmlTextReader(strXmlFile);
     XmlValidatingReader objXmlValRdr = new XmlValidatingReader(objXmlTxtRdr);
     
     // set the validation type
     objXmlValRdr.ValidationType = ValidationType.DTD;
     
     // set the validation event handler
     objXmlValRdr.ValidationEventHandler += new ValidationEventHandler
    (ValidationMonitor);
     
     // some output
     output.Text = "Validating file: <b>" + strXmlFile.ToString() + "</b><br>";
     
     // read XML data
     while (objXmlValRdr.Read()){
     

      
    String strSpaces;
      

      
    // only process the elements, ignore everything else
              if(objXmlValRdr.NodeType==XmlNodeType.Element) {
            
               // reset the variable for a new node
               strSpaces = "";
                

        
    for(int count 1count <= objXmlValRdr.Depthcount++) {
        strSpaces 
    += "===";
        
    }
     

       output
    .Text += strSpaces "=> " objXmlValRdr.Name "<br/>";
         
    }
     
    }
     
     
    output.Text += "Validation <b>" + (blnValidationSuccess == true ?
    "successful" "failed") + ".</b>";
      

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

    When you test this code, you'll see that the XML document instance is successfully validated against the "library.dtd" file:

    XMLReader Object

    Now, once again, let me spoil things by introducing a rogue <inventory> element into the XML:

    XMLReader Object

    As you can see, the XmlValidatingReader object is quick to complain about the presence of the unwanted <inventory> element on the basis of the definitions present in the accompanying library.dtd file.

    So what makes this script click? To be frank, the code hasn't changed much from my previous example. The major difference lies in the ValidationType property of the XMLValidatingReader object; I have updated it to use a DTD instead of an XML Schema, as shown below:
      


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

    And to make things more interesting, I have added some code to the Read() function to prove that you need not leave it blank -- a while loop now prints the names of elements to the console:


    <%
     
    // snip
     
    // read XML data
    while (objXmlValRdr.Read()) {
     
     
    String strSpaces;
      

     
    // only process the elements, ignore everything else
     if(objXmlValRdr.NodeType == XmlNodeType.Element) {
            

      // reset the variable for a new node
             strSpaces = "";
               

      
    for(int count 1count <= objXmlValRdr.Depthcount++) {
       strSpaces 
    += "===";
      
    }
     

      output
    .Text += strSpaces "=> " objXmlValRdr.Name "<br/>";
        
    }
    }
     
    // snip
    %>

    It is interesting to note here that the XMLValidatingReader will continue to read the XML data even if it encounters an error - which is why it becomes critical to ensure that you devise your very own escape route to get out of erroneous situations.

    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 1 hosted by Hostway
    Stay green...Green IT