Logo         Community
The Company
Your Account
Contact Us
Creating XML Trees With The XmlTextWriter and XmlDocument Objects
Dynamically create well-formed XML documents in your ASP.NET scripts

| Breaking It Down |

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

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

// 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

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

} 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) {


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback