Recipe 15.5. Creating an XML Document ProgrammaticallyProblemYou have data that you want to put into a more structured form, such as an XML document. SolutionSuppose you have the information shown in Table 15-2 for an address book that you want to turn into XML.
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. DiscussionNow 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 AlsoSee the "XmlDocument Class," "XML Document Object Model (DOM)," "XslCompiledTransform Class," and "IXPathNavigable Interface" topics in the MSDN documentation. |