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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 1
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2004-02-02

    Table of Contents:
  • XML Processing with the XMLReader Object, Part 1
  • Class Act
  • Visiting the Library
  • Digging Deeper
  • Into the Real World

  • 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 1 - Visiting the Library


    (Page 3 of 5 )

    I'll begin with a simple example - using an XmlTextReader to parse a static XML file. Here's the XML file, a list of books present in our technical library:


    <?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>
     </book>
    </library>

    And now for the ASP.NET code that will allow us to parse this XML file using the XmlTextReader object:


    <%@ Page Language="C#"%>
    <%@ import  namespace="System.Xml"%>
    <html>
    <head>
    <script runat="server">
    void Page_Load
    () 
    {
     
    // location of XML file
     string strXmlFile = 
     "http://localhost/xmlpull/library.xml";
     
     // create an instance of the 

     // XmlTextReader object
     XmlTextReader objXmlRdr = new XmlTextReader(strXmlFile);
     
     // ignore whitespace in the XML file
     objXmlRdr.WhitespaceHandling=WhitespaceHandling.None;
     
     
    String strSpaces;
     
     while(
    objXmlRdr.Read()) {
     
      
    // only process the elements, 
      // ignore everything else
      if(objXmlRdr.NodeType==XmlNodeType.Element) {
            

      // reset the variable for a new node
      strSpaces = "";
     
      
    for(int count 1
      count 
    <= objXmlRdr.Depth
      count
    ++) {
       strSpaces 
    += "===";
      
    }
      output
    .Text += strSpaces 
      
    "=> " objXmlRdr.Name 
      
    "<br/>";
     
    }
    }
     
    // close the object and free up memory
    objXmlRdr.Close();     
    }
    </script>
    </head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>

    Before I get into the nitty-gritty of the code, here's what you should see when you run this script:

    XMLReader

    1. The first step is to import all the classes required to execute the application - the .NET libraries for the XML parser, which are part of the System.XML namespace.


    <%@ import  namespace="System.Xml"%>

    2. Next, within the Page_Load() function, I have defined some variables and objects. The first is a string variable to store the location of the XML file, and the second is a local instance of the XmlTextReader object.

    3. Finally, in order to tell the parser to ignore the whitespace present in the XML file, I set the "WhitespaceHandling" property of the XmlTextReader object to "None", as shown below:


    <%
    // location of XML file
    string strXmlFile = "http://localhost/xmlpull/library.xml";
     
    // create an instance of the 
    // XmlTextReader object
    XmlTextReader objXmlRdr = 
    new XmlTextReader(strXmlFile);
     
    // ignore whitespace in the XML file
    objXmlRdr.WhitespaceHandling = 
    WhitespaceHandling.None;
    %>

    4. The next step is to read the XML file - a simple matter, since the object provides a Read() method for just this purpose. This method returns true if it encounters a node in the XML file. Once it is finished with the file, it returns false. This makes it easy to process an entire file, simply by wrapping the method call in a "while" loop.


    <%
    while
    (objXmlRdr.Read()) {
     
    // process the XML data
     }    
    %>

    5. Of course, it doesn't make sense to read the entire file and not do anything with it. That's why, within the "while" loop, I've added the code to process element nodes and format them for display.


    <%
    while(
    objXmlRdr.Read()) {
     
     
    // only process the elements
     if(objXmlRdr.NodeType==XmlNodeType.Element) {
     
      // reset the variable for a new node
      strSpaces = "";
     
      
    for(int count 1
      count 
    <= objXmlRdr.Depth
      count
    ++) {
       strSpaces 
    += "===";
      
    }
      output
    .Text += strSpaces 
      
    "=> " objXmlRdr.Name 
      
    "<br/>";
     
    }
    }    
    %>

    The "NodeType" property of the current node can be used to filter out the elements for further processing. Note that if I hadn't included this condition at the beginning of the loop, the output would also contain processing instructions like this:


    <?xml version='1.0'



    Don't take my word for it - change the code and see for yourself!

    The rest of the code in the "while" loop ensures that the output is formatted properly for display in the browser. Pay special attention to my use of the very cool "Depth" property, which holds an integer value specifying the depth of the current node in the tree hierarchy. Simply put, the element <library> is at depth 0, the element <book> is at depth 1, and so on.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT