Suppose you have the following information for an address book that you want to turn into XML:
Use the
XmlTextWriter
to create XML for this table:
XmlTextWriter writer = new XmlTextWriter(Console.Out); writer.Formatting = Formatting.Indented; writer.WriteStartElement("AddressBook"); writer.WriteStartElement("Contact"); writer.WriteAttributeString("name", "Tim"); writer.WriteAttributeString("phone", "999-888-0000"); writer.WriteEndElement( ); writer.WriteStartElement("Contact"); writer.WriteAttributeString("name", "Newman"); writer.WriteAttributeString("phone", "666-666-6666"); writer.WriteEndElement( ); writer.WriteStartElement("Contact"); writer.WriteAttributeString("name", "Harold"); writer.WriteAttributeString("phone", "777-555-3333"); writer.WriteEndElement( ); writer.WriteEndElement( ); writer.Close( );
Or you can use the
XmlDocument
class to programmatically construct
XML from other data:
public static void CreateXML( ) { // Start by making an XmlDocument XmlDocument xmlDoc = new XmlDocument( ); // create a root node for the document XmlElement addrBook = xmlDoc.CreateElement("AddressBook"); xmlDoc.AppendChild(addrBook); // create the Tim contact XmlElement contact = xmlDoc.CreateElement("Contact"); contact.SetAttribute("name","Tim"); contact.SetAttribute("phone","999-888-0000"); addrBook.AppendChild(contact); // create the Newman contact contact = xmlDoc.CreateElement("Contact"); contact.SetAttribute("name","Newman"); contact.SetAttribute("phone","666-666-6666"); addrBook.AppendChild(contact); // create the Harold contact contact = xmlDoc.CreateElement("Contact"); contact.SetAttribute("name","Harold"); contact.SetAttribute("phone","777-555-3333"); addrBook.AppendChild(contact); // Display XML Console.WriteLine("Generated XML: {0}",addrBook.OuterXml); Console.WriteLine( ); }
Both of these methods generate XML that looks like this:
<AddressBook> <Contact name="Tim" phone="999-888-0000" /> <Contact name="Newman" phone="666-666-6666" /> <Contact name="Harold" phone="777-555-3333" /> </AddressBook>
Now that you have seen two ways to do this, the question arises:
“Which one to use?”
The XMLDocument
uses
the traditional DOM method of interacting with XML, while the
XmlTextReader
/XmlTextWriter
combination deals with XML in a streaming format. If you are dealing
with larger documents, you are probably better off using the
XmlTextReader
/XmlTextWriter
combination than the XmlDocument
. The
XmlTextReader
/XmlTextWriter
combination is the better-performing of the two when you do not need
the whole document in memory. If you need the power of being able to
traverse back over what you have written already, use
XmlDocument
.
XmlDocument
is the class that implements the
DOM model for XML
processing in the .NET Framework. The DOM holds all of the nodes in
the XML in memory at the same time, which enables tree traversal both
forward and backward. DOM also allows for a writable interface to the
whole XML document, which other XML classes do not provide in .NET.
XmlDocument
allows you to manipulate any aspect of
the XML tree, is eligible to be used for XSLT transformations via the
XslTransform
class through its support of the
IXPathNavigable
interface, and allows you to run
XPath
queries against the document without having
to create an XPathDocument
first.