XML
  Home arrow XML arrow Page 4 - 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 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 - Digging Deeper


    (Page 4 of 5 )

    So that takes care of handling elements, but what about the attributes contained within each element? Take a look at this second example, which demonstrates how to process attributes using the XmlTextReader class:


    <%@ Page Language="C#"%>
    <%@ import  namespace="System.Xml"%>
    <html>
    <head>
    <script runat="server">
    void Page_Load
    ()  {
     
     
    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 
      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;
     
     
    // check if the element has any 
     // attributes
     if(objXmlRdr.HasAttributes)
     {
      output.Text += " [";
      for(int innercount = 0; 
      innercount < objXmlRdr.AttributeCount;
      innercount++) {
     
       // read the current attribute
       objXmlRdr.MoveToAttribute(innercount);
       output.Text += objXmlRdr.Name;
      }
     
      
    output.Text += "]";
     
      
    // instruct the parser to go back 
      // the element 
      objXmlRdr.MoveToElement();
     }
    output.Text += "<br/>";
     }    
    }
    // close the object and free up memory
    objXmlRdr.Close();
    }
    </script>
    </head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>

    Here's the output:

    XMLReader

    As you can see, there is only one major change to the original code listing - handling attributes for each element that the reader encounters in the XML file:


    <%
    // check if the element has 
    // any attributes
    if(objXmlRdr.HasAttributes)  {
     
     
    output.Text += " [";
     
    for(int innercount 0
     innercount 
    objXmlRdr.AttributeCount;
     innercount
    ++) {
        

      
    // read the current attribute
      objXmlRdr.MoveToAttribute(innercount);
      output.Text += objXmlRdr.Name;
     }
     
     
    output.Text += "]";
     
     
    // instruct the parser to 
     // go back the element 
     objXmlRdr.MoveToElement();
    }
     
    %>

    The above code snippet makes for interesting reading. It begins with a check for attributes in the current node using the "HasAttributes" property (this property is set to true if the current node has at least one attribute). The XmlTextReader object's "AttributeCount" property stores the total number of attributes and is useful for looping through the collection of attributes. The MoveToAttribute() method positions the reader at the next attribute in the collection, and the "Name" property is then used to get the name of the attribute. Once iteration through the attributes of the current node is complete, the MoveToElement() method resets the position of the reader, and it then proceeds to the next node (if it exists).

    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