Recipe15.5.Creating an XML Document Programmatically


Recipe 15.5. Creating an XML Document Programmatically

Problem

You have data that you want to put into a more structured form, such as an XML document.

Solution

Suppose you have the information shown in Table 15-2 for an address book that you want to turn into XML.

Table 15-2. Sample address book data

Name

Phone

Tim

999-888-0000

Newman

666-666-6666

Harold

777-555-3333


Use the XmlWriter to create XML for this table:

     XmlWriterSettings settings = new XmlWriterSettings();     settings.Indent = true;     using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))     {         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();     } 

This method will give you output 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> 

Or you can use the XmlDocument class to programmatically construct the XML:

 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:\r\n{0}",addrBook.OuterXml);     Console.WriteLine( ); } 

This method gives the output like this:

 Generated XML: <AddressBook><Contact name="Tim" phone="999-888-0000" /><Contact name="Newman" phone="666-666-6666" /><Contact name="Harold" phone="777-555-3333" /></AddressBook> 

Both methods produce the same XML, but the first method is formatted with indents.

Discussion

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 XmlReader/XmlWriter combination deals with XML in a stream. If you are dealing with larger documents, you are probably better off using the XmlReader/XmlWriter combination than the XmlDocument. The XmlReader/XmlWriter 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 or write items out of order, 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. It is also eligible to be used for XSLT transformations via the XslCompiledTransform class, through its support of the IXPathNavigable interface. It allows you to run XPath queries against the document without having to create an XPathDocument first.

See Also

See the "XmlDocument Class," "XML Document Object Model (DOM)," "XslCompiledTransform Class," and "IXPathNavigable Interface" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net