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();
}
}
}
%>
Next: The Real Thing >>
More XML Articles
More By Harish Kamath (c) Melonfire