2006-03-05
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 2 of 3 )
XML document">
I've been working with Web services lately. After writing a bunch of code, I found a way to trick the .NET compiler into treating an XML stream as HTML. Doing so makes managing XML data a lot easier, as you can feed data returned from the web service right into your .Net script without having to deserialize, create, or parse an XML doc just to get this data. That would be a lot of extra work.
If you are working with web services or writing applications that share data with other proprietary applications, no doubt you, too, will use XML to accomplish these tasks. In this article, I show you the fundamentals of working with XML using some of the more important properties of the System.XML Namespace.
Creating and Populating an XML document
Your application may need to write values to an XML document that is used by other applications, perhaps in different environments. To help you with this, the XML Namespace has a class called XmlTextWriter. XmlTextWriter has methods for declaring the new document and for writing the attributes and elements of the XML code that comprise the new file.
The example below illustrates the process:
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Xml" %> <%@ Page Language="C#" %>
Set up a string for the filename, and create a new type called writer that you will use to access the methods of the XmlTextWriter class.
string xmlFilename = Server.MapPath("Authors.xml")";
writer = new XmlTextWriter(xmlFilename, System.Text.Encoding.UTF8);
Use the Indented property of the Formatting method to start an indent for readability.
writer.Formatting = System.Xml.Formatting.Indented;
Declare the document with the WriteStartDocument() method. When you call this method, the writer makes sure that the new XML document is well-formed; it verifies that only one root element exists, and so on.
writer.WriteStartDocument();
Now, we build the file from the top down, using the other methods for writing elements and attributes. We start by creating the root element:
writer.WriteStartElement("Authorlist");
Next, write out the parent and child elements of the XML doc. In this example, Authors is a parent to the child node Title. Inside the parent node are some attributes.
writer.WriteStartElement("Authors");
writer.WriteAttributeString("au_id", "409-56-7008");
writer.WriteAttributeString("au_lname", "Bennet");
writer.WriteAttributeString("au_fname", "Abraham");
writer.WriteElementString("Title","The Busy Executive's Database Guide");
writer.WriteEndElement();
Finally, write out the closing element tag to the root node. Close the file and the writer.
writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close();
After executing the script, the following XML results are produced:
<?xml version="1.0" encoding="utf-8"?>
<Authorlist>
<Authors au_id="409-56-7008" au_lname="Bennet" au_fname="Abraham">
<Title>T</Title>
</Authors>
</Authorlist>
You can use this code as template to write any values from your application to an XML file, if you replace the above example with database code to fill in the blanks.
![]() |
|


