- XML Reader Navigation
- Reading Attributes
- Movement and Advanced Reads
- Creation Using XMLWriter
- Validating Your Work
- Summary
Creation Using XMLWriter
A drawback of the XmlReader API is that it is designed for read-only access to the XML document. The DOM allows for navigation and creation in one API. You must use the XmlWriter API to omit an XML document from a client program. Fortunately, it is easy to use and is light on system resources. The API uses a "push" model that immediately creates the output content and doesn't cache or retain anything in memory. This list shows some of the commonly used output method calls in the XmlWriter API:
- WriteStartDocument
- WriteAttributeString
- WriteStartElement
- WriteString
- WriteEndElement
- WriteElementSTring
- WriteEndDocument
The API relies upon a set of WriteStart and WriteEnd calls for types that have subordinate content. The API also provides the capability to write out simple textual content with special write methods: WriteAttributeString and WriteElementString are the easiest ways to build attributes and elements with text content.
The example in Listing 9 shows you how to build the purchase order document manually using the XmlTextWriter. The XmlTextWriter class is a concrete implementation of XmlWriter that produces textual output. Notice how you can produce indentation by the appropriate setting of the Formatting property of the XmlTextWriter. Also notice the code reduction from the DOM creation code in the previous article.
Listing 9: XmlWriter Purchase Order Creation
using System; using System.Xml; namespace XmlDemos { public class WriteCreateDefault { public static void Main() { Console.WriteLine("Building PO doc..."); Console.WriteLine(); // link up the writer to the console XmlTextWriter writer = new XmlTextWriter(Console.Out); // format the output with nice indentation writer.Indentation = 5; writer.Formatting = Formatting.Indented; // <xml> writer.WriteStartDocument(); // PurchaseOrder writer.WriteStartElement("PurchaseOrder"); // Number writer.WriteElementString("Number","","1002"); // OrderDate writer.WriteElementString("OrderDate","","8/13/01"); // BillToAddress WriteAddress(writer, "BillToAddress", "101 Main Street", "Charlotte", "NC", "28273"); // ShipToAddress WriteAddress(writer, "ShipToAddress", "101 Main Street", "Charlotte", "NC", "28273"); // LineItem WriteLineItem(writer, "Computer Desk", "Wood desk for computer", "12345A123", 499.99M, 1); writer.WriteEndElement(); writer.WriteEndDocument(); Console.WriteLine(); Console.WriteLine(); } public static void WriteAddress(XmlWriter writer, string name, string street, string city, string state, string zipcode) { // Address writer.WriteStartElement(name); // Street writer.WriteElementString("Street","",street); // City writer.WriteElementString("City","",city); // State writer.WriteElementString("State","",state); // ZipCode writer.WriteElementString("ZipCode","",zipcode); writer.WriteEndElement(); } public static void WriteLineItem(XmlWriter writer, string name, string description, string SKU, decimal price, int quantity) { // LineItem writer.WriteStartElement("LineItem"); // name writer.WriteAttributeString("","name","",name); // SKU writer.WriteAttributeString("","SKU","",SKU); // price writer.WriteAttributeString("","price","",Convert.ToString(price)); // quantity writer.WriteAttributeString("","quantity", "",Convert.ToString(quantity)); writer.WriteEndElement(); } } }
Listing 10: XmlWriter Purchase Order Creation Output
Building PO doc... <?xml version="1.0" encoding="IBM437"?> <PurchaseOrder> <Number>1002</Number> <OrderDate>8/13/01</OrderDate> <BillToAddress> <Street>101 Main Street</Street> <City>Charlotte</City> <State>NC</State> <ZipCode>28273</ZipCode> </BillToAddress> <ShipToAddress> <Street>101 Main Street</Street> <City>Charlotte</City> <State>NC</State> <ZipCode>28273</ZipCode> </ShipToAddress> <LineItem name="Computer Desk" SKU="12345A123" price="499.99" quantity="1" /> </PurchaseOrder>