XML
  Home arrow XML arrow Page 4 - 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 - The Real Thing


    (Page 4 of 7 )

    As you must have figured out by now, using the XmlTextWriter object is fairly easy. In the introductory example, I demonstrated how you can write an XML file without much fuss; in this next one, I'll go much further, demonstrating how the writer's built-in properties and methods can be used to write a "real" XML file.


    <%@ Page Language="C#" Debug="true" %>
    <%@ import  namespace="System.Xml"%>
    <html>
    <head>
    <script runat="server">
    void Page_Load
    () {
     
     
    // initialize a XmlTextWriter object
      XmlTextWriter objXmlWriter = null; 
     
     // path to the XML file 
     String strXmlFile = "E:/Inetpub/wwwroot/xml/library.xml";
     
     // start the "try" block
     try {
     
      // create an instance of the XmlTextWriter object
      objXmlWriter = new XmlTextWriter (strXmlFile , null);
     
      // indent the output in the XML file
      objXmlWriter.Formatting = Formatting.Indented;
     
      // set the number of space to indent
      objXmlWriter.Indentation =  5;
     
      // start writing the XML document
      objXmlWriter.WriteStartDocument(false);
     
      // write a comment 
      objXmlWriter.WriteComment("This is a list of the books written by Melonfire.");  
     
      // start writing the elements
      objXmlWriter.WriteStartElement("library");
      objXmlWriter.WriteStartElement("book", null);
      objXmlWriter.WriteAttributeString("bkid","b1");
     
      
    objXmlWriter.WriteElementString("title""XML and PHP");
      objXmlWriter
    .WriteElementString("author""Vikram
    Vaswani"
    );
     
      
    objXmlWriter.WriteStartElement("description"null);
      objXmlWriter
    .WriteCData("Learn to manage your XML data
    with PHP"
    );
      objXmlWriter
    .WriteEndElement();
     
      
    objXmlWriter.WriteStartElement("price"null);
      objXmlWriter
    .WriteAttributeString("currency","USD");
      objXmlWriter
    .WriteString("24.95");
      objXmlWriter
    .WriteEndElement();
     
      
    // close each element 
      objXmlWriter.WriteEndElement(); // the "book" element
      objXmlWriter.WriteEndElement();  // the "library"
    element
     
      // flush and write 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();
      }   
     }         
    }
    </script>
    </head>
    <body>
    <asp:label id="output" runat="server" text="It's all done." />
    </body>
    </html>

    Here's the XML output generated by the above code listing.


    <?xml version="1.0" standalone="no"? >
    <!--This is a list of the books written by Melonfire.-->
    <library>
         
    <book bkid="b1">
              
    <title>XML and PHP</title>
              
    <author>Vikram Vaswani</author>
              
    <description><![CDATALearn to manage your XML data with PHP
    ]]></description>
              
    <price currency="USD">24.95</price>
         
    </book>
    </library>

    Looks a lot neater now, doesn't it?

    Let's now look closely at the changes that I made to the first example to bring about this amazing transformation:


    <%
     
     
    // create an instance of the XmlTextWriter object
     objXmlWriter = new XmlTextWriter (strXmlFile , null);
     
     // indent the output in the XML file
     objXmlWriter.Formatting = Formatting.Indented;
     
     // set the number of space to indent
     objXmlWriter.Indentation =  5;
     
    %>

    First up, the Formatting property of the XmlTextWriter object. If this property is set to Formatting.Indented, the XML output created by the class is indented (you can also use the Indentation property to control the amount of indentation, you control freak, you!).


    <%
     
     
    // write a comment 
     objXmlWriter.WriteComment("This is a list of the books written by Melonfire."); 
     
    %>

    It's obvious that the WriteComment() method is used to insert comments (hopefully meaningful) into the XML file. A good practice in general, this becomes a necessity if your XML file is widely distributed.


    <%
     
     
    objXmlWriter.WriteElementString("title""XML and PHP");
     objXmlWriter
    .WriteElementString("author""Vikram Vaswani");
     
     
    objXmlWriter.WriteStartElement("description"null);
     objXmlWriter
    .WriteCData("Learn to manage your XML data with
    PHP"
    );
     objXmlWriter
    .WriteEndElement();
     
     
    objXmlWriter.WriteStartElement("price"null);
     objXmlWriter
    .WriteAttributeString("currency","USD");
     objXmlWriter
    .WriteString("24.95");
     objXmlWriter
    .WriteEndElement();
     
    %>


    As you can see from the above, the <title> and <author> elements have been created using the WriteElementString() I showed you earlier. However, when it comes to more descriptive text, which is better stored as a CDATA element, you might want to use the WriteCData() method instead.

    If you need to introduce an attribute at the last level of the XML hierarchy, opt for


    <%
     
     
    objXmlWriter.WriteStartElement("price"null);
     objXmlWriter
    .WriteAttributeString("currency","USD");
     objXmlWriter
    .WriteString("24.95");
     objXmlWriter
    .WriteEndElement();
     
    %>


    Here, things start with a call to the WriteStartElement() method to write the starting element <price>. Now, I can introduce the currency attribute by invoking WriteAttributeString(), and write a value to it with the WriteString() method. The WriteEndElement() method wraps things up neatly by closing the element.

    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
    Stay green...Green IT