XML
  Home arrow XML arrow Page 2 - 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 
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

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 - Spending Time in the Library


    (Page 2 of 7 )

    The XmlTextWriter object can best be considered as a counterpart to the XmlTextReader object, allowing you to perform the reverse function. The next example shows you how:


    <%@ Page Language="C#" Debug="true" %>
    <%@ import  namespace="System.Xml"%>
    <html>
    <head>
    <script runat="server">
    void Page_Load
    () {
     
     
    // initialize a XmlTextWriter object
      XmlTextWriter objXmlWriter = null; 
     
     // location to the XML file to write
     String strXmlFile = "E:/Inetpub/wwwroot/xml/library.xml";
     
     // start the "try" block
     try {
     

      
    objXmlWriter = new XmlTextWriter (strXmlFile null);
     
      
    // start writing the XML document
      objXmlWriter.WriteStartDocument(false);
     
      // start with the root element
      objXmlWriter.WriteStartElement("library");
     
      // first child element
      objXmlWriter.WriteStartElement("book", null);
     
      // add an attribute
      objXmlWriter.WriteAttributeString("bkid","MFRE001");
     
      // some text
      objXmlWriter.WriteElementString("title", "XML and PHP");
     
      // back to the root element
      // closing each element written above
      // one at a time
      objXmlWriter.WriteEndElement();
      objXmlWriter.WriteEndElement();
     
      // 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();
      }   
     }         
    }
    </script>
    </head>
    <body>
    <asp:label id="output" runat="server" text="It's all done!" />
    </body>
    </html>

    Run this example in your browser, and you might see something like this:

    A General Exception occurred: Access to the path
    "E:/Inetpub/wwwroot/xml/library.xml" is denied.

    Don't despair! This is just a matter of giving appropriate permissions on the target directory to the user. But which user?

    According to the official ASP.NET documentation, the "ASP.NET V1 RTM now runs using a less privileged Windows account, registered as the ASPNET account on a local machine" (http://www.asp.net/security.aspx?tabindex=0&tabid=1).

    So, all this means that you need to locate the "ASPNET" user in your user management control panel and give the corresponding user account permission to write to the target directory. Once that's done, run the example again, and you will now see a more cheerful message:

    It's all done.

    In reality, the action takes place elsewhere.

    Navigate to the location specified in the script and look for a file called library.xml. This is what it should look like.


    <?xml version="1.0" standalone="no"? ><library><book
    bkid
    ="MFRE001"><title>XML and PHP</title></book></library>

    At first glance, this is very unattractive to the naked eye. I will show you later how this can be formatted to look a little nicer. First, though, it's time for a quick anatomy lesson on the code above.

    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