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 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 - Of Nodes and Trees


    (Page 4 of 6 )

    Now, you've already seen two of the three built-in objects based on the core XmlReader object -- the XmlTextReader and XmlValidatingReader objects. This leaves us with the third and final object in this family to deal with: the XmlNodeReader object.

    The XmlNodeReader class allows you to read data from any node of a DOM tree present in memory. Now you may, as I did initially, question the rationale behind having a reader for such a specific requirement -- after all, you can easily use the method and properties of the XmlDocument object to parse the file. But it's important to remember that DOM parsing is a processor-intensive task; therefore, using an XmlReader object (which is faster and not as resource-hungry as the regular DOM parser) can produce better results in some cases.

    Second, it is not wise to assume that your application will always receive XML data in the form of a flat file or an XML data stream. XML data might even come to you in the form of a fragment of DOM tree. In such a scenario, it makes sense to use the XmlNodeReader object to read the contents of the node because of its speed and efficient performance.

    Enough talk! Take a look at an example that uses the XmlNodeReader object.


    <%@ Page Language="C#"%>
    <%@ import  namespace="System.Xml"%>
    <html>
    <head>
    <script runat="server">
    void Page_Load
    () {
     
     
    // define some variables
     string strXmlFile = http://localhost:2121/xmlpull/library.xml;
     
     // create an instance of the XmlDocument object
     XmlDocument objXmlDoc = new XmlDocument();
     
     // load an XML file into the XmlDocument object
     objXmlDoc.Load(strXmlFile);
     
     // load the NodeList object with the nodes required
     XmlNodeList objNodeList = objXmlDoc.GetElementsByTagName("book");
     

     // loop through the node list 
     // for each node, create an XmlNodeReader object
     // to read the XML data from the file
     foreach(XmlNode myNode in objNodeList) {
     

      
    XmlNodeReader objXmlRdr = new XmlNodeReader(myNode);
      ReadXmlNode
    (objXmlRdr);
      objXmlRdr
    .Close();
     
    }
    }
     
    void ReadXmlNode(XmlNodeReader objXmlRdr) {
     
    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 1count <= objXmlRdr.Depthcount++) {
        strSpaces 
    += "===";
        
    }
       

      output
    .Text += strSpaces "=> " objXmlRdr.Name "<br/>";
      
    }
     
    }

    </script>

    </head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>

    This example is pretty straightforward. First, I have loaded the XML file into an XMLDocument object with the Load() method. I have then drilled down to the node of interest with the convenient GetElementsByTagName() method, which returns an XmlNodeList object that I can iterate over using a foreach loop, as shown below.

    Now, each item in the XmlNodeList collection is an XmlNode object that can easily be used to create an instance of the XmlNodeReader object, the object that I'm interested in here. Since there will be multiple nodes resulting from this process, it make sense to place the corresponding code in a separate function which can be invoked repeatedly; hence the ReadXmlNode() function in the example above. Take a close look at this function, and you'll see that there isn't much difference between the method and properties of the XmlNodeReader object and the XmlTextReader object (the main difference lies in the source of the XML data they are capable of accepting)

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