XML
  Home arrow XML arrow Page 5 - 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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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 - Playing Catch


    (Page 5 of 6 )

    To err is human -- which is why it's imperative that you include some mechanism in your ASP.NET code to handle errors that come up during script execution. And the next example does just that, using the ever-popular "try-catch" mechanism to trap any errors that might arise. Take a look:


    <%@ Page Language="C#"%>
    <%@ import  namespace="System.Xml"%>
    <html>
    <head>
    <script runat="server">
    void Page_Load
    () {
     
     
    // create the XML Reader object
      XmlTextReader objXmlRdr = null;
     
     // start the "try" block
     try {
     
      // location of XML file
      string strXmlFile = "http://localhost:2121/xmlpull/library.xml";
          String strSpaces;     
     
      // create an instance of the XmlTextReader object
      objXmlRdr = new XmlTextReader(strXmlFile);
      objXmlRdr.WhitespaceHandling=WhitespaceHandling.None;
     
      
    while(objXmlRdr.Read()) {
     
            
    // only process the elements, ignore everything else
               if(objXmlRdr.NodeType==XmlNodeType.Element) {
     
        
    strSpaces "";
         
    for(int count 1count <= objXmlRdr.Depthcount++) {
         strSpaces 
    += "===";
         
    }
     
          
    output.Text += strSpaces "=> " objXmlRdr.Name "<br/>";
          
    }
      
    }
     
     } catch (
    XmlException e) {
     
      
    output.Text "An XML Exception occurred: " e.Message;
     
     } catch (
    Exception e) {
     
      
    output.Text "A General Exception occurred: " e.Message;
     
     } 
    finally {
     
      
    // close the XMLReader object
      // if it exists
      if(objXmlRdr != null) {
       objXmlRdr.Close();
      }   
     }         
    }
    </script>
    </head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>

    If all goes well, the output shows the tree structure of the XML document instance. But now, introduce an deliberate error by deleting the library.xml file and look what happens:

    A General Exception occured: The remote server returned an error: (404) Not Found.

    Notice how the script take note of the absence of the XML file and displays a polite little message informing the user about the error.

    Here's what you'd see if you didn't have an error-handling mechanism in place:

    XMLReader Object

    Not a pleasant sight at all!

    Let's try another error -- "forget" to close the <library> element at the end of the file (thereby creating an XML document instance that is not well-formed) and look how the exception-handling mechanism reacts:

    An XML Exception occurred: This is an unexpected token. The expected token is 'EndElement'. Line 15, position 3.

    Most of the magic here lies in the "try-catch-finally" block, which does all the dirty work.


    <%
     
    // snip
     
    // start the "try" block
    try {
     
     // process the XML file
     // snip 
     
    } catch (XmlException e) {
    output
    .Text "An XML Exception occurred: " e.Message;  
     
    } catch (
    Exception e) {
     output
    .Text "A General Exception occurred: " e.Message;
    finally {
     
     
    // close the XMLReader object
     // if it exists
     if(objXmlRdr != null) {
      objXmlRdr.Close();
     }   
    }         
     
    // snip
     
    %>

    First, you place all the code that processes the XML file -- creating the XmlTextReader object, loading and reading the XML file and so on -- in the "try" block.

    This is followed by two catch blocks, one to handle an XmlException (these occur if something is wrong with the XML file itself) and another to handle any general Exception (such as a missing file).

    Finally (pun intended), the finally block, which contains code that will always execute at the end of the "try" block (even if an exception takes place). This is the place for the code that closes objects and frees up vital system resources.

    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 4 hosted by Hostway