XML
  Home arrow XML arrow Page 3 - Creating XML Trees with the XmlTextWriter ...
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

Creating XML Trees with the XmlTextWriter and XmlDocument Objects
By: Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2004-04-21

    Table of Contents:
  • Creating XML Trees with the XmlTextWriter and XmlDocument Objects
  • Spending Time in the Library
  • Breaking It Down
  • The Real Thing
  • Walking the DOM
  • Adding More
  • 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


    Creating XML Trees with the XmlTextWriter and XmlDocument Objects - Breaking It Down


    (Page 3 of 7 )

    Here is a step-by-step explanation of the code listing on the previous page:

    1. The first step is to import all the classes required for the application. I'll begin with the .NET libraries for the XML parser:


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

    2. Within the Page_Load() function, I start by defining some variables and objects. The first is a local instance of the XmlTextWriter object, and the second is a string variable to store the location of the XML file. Note that you must give an absolute file path here (a relative path or a URL won't work).  


    <%
     
    // initialize a XmlTextWriter object
      XmlTextWriter objXmlWriter = null; 
     
     // location to the XML file to write
     String strXmlFile = "E:/Inetpub/wwwroot/xml/library.xml";
    %>

    3. Within an efficient little try-catch block, I now create an instance of the XmlTextWriter object, and begin writing the XML document instance by invoking the WriteStartDocument() method. This writes the opening XML declaration to the file.


    <%
      objXmlWriter 
    = new XmlTextWriter(strXmlFile null);
      

      
    // start writing the XML document
      objXmlWriter.WriteStartDocument(false);
    %>

    4. Next comes the (slow!) process of building the XML document by adding elements to it one-by-one using the WriteStartElement() method. This method takes only one argument -- the element name -- and hence cannot be used to write elements that contain character data.

    The mirror image of the WriteStartElement() method is the WriteEndElement() method, which takes care of writing corresponding end elements to the XML document. Note that it is essential to get the order of method calls correct here, or else your XML output will not be well-formed...and we all know what a sin that is!


    <%
     
     
    // start with the root element
     objXmlWriter.WriteStartElement("library");
       

     // first child element
     objXmlWriter.WriteStartElement("book", null);
     
     // more of the same
         

     // traverse back to the root element
     // closing each element written above
     // one at a time
     objXmlWriter.WriteEndElement();
     objXmlWriter.WriteEndElement();
    %>

    5. Of course, writing elements without content may be a great deal of fun, but it isn't actually very useful...which is why there are also some methods that actually write data into the XML file.

    First, the WriteElementString() method, which requires two parameters: the name of the element and the data to be contained within it. Note that you don't have to worry about closing elements written in this manner –- the WriteElementString() method does all the work for you!


    <%
     
    // first child element
     objXmlWriter.WriteStartElement("book", null);
     
     // add an attribute
     objXmlWriter.WriteAttributeString("bkid"," MFRE001");  
     
     // an element with some text
     objXmlWriter.WriteElementString("title", "XML and PHP");
         

    %>

    What about attributes, you ask? No sweat, the XmlTextWriter class comes equipped with a handy WriteAttributeString() method for just that purpose. For example, the code above uses this method to add a bkid attribute to the <book> element.

    6. To wrap things up, the Flush() method actually writes the XML data stream that has been building in memory to a file. This is followed by a set of catch block to trap errors and gracefully exit, and a finally block that closes the XmlTextWriter object and frees up system resources for other activities.


    <%
     
    try {
     
      
    // snip
     
      // flush the object and write the
      // XML data to the file
      objXmlWriter.Flush();
      

     
    } 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 XMLWriter object
      if(objXmlWriter != null) {
       objXmlWriter.Close();
      }   
     }         
    }
     
    %>

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