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><![CDATA[ Learn 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.
Next: Walking the DOM >>
More XML Articles
More By Harish Kamath (c) Melonfire