Typically the XmlTextWriter is used if you need to write XML as 'raw' data without the overhead of a DOM and therefore provides a faster mechanism for writing XML.
The XmlWriter class is the API which provides the XML writing to file, stream or a TextWriter. This provides numerous validation and checking rules to ensure
that the written XML is well formed. When certain violations occur exceptions are thrown, which should be caught. The XmlTextWriter has different constructors
to specify the location to write the XML data to. In this sample we are going to write XML to the
The sample code below constructs an XmlTextWriter with a string representing the file location.
<div class="code"><xmp>
XmlTextWriter writer = new XmlTextWriter ("newbooks.xml", null);
</xmp></div>
<div style="width:660">
The constructor takes the filename that you want to write to, and the encoding that you would like to generate. If encoding is 'null' it writes out UTF-8.
The XML 1.0 specification details how XML documents are encoded.</div>
<br clear="left"><br><div style="width:660">
Here we are going to write another fragment of the bookstore XML file. For each XML type there are corresponding XML write methods.
For example, to write an element use the <b>WriteElementString</b> method and to write an attribute use the <b>WriteAttribute</b> method.
For nested levels the <b>WriteStartElement</b>/<b>WriteEndElement</b> pair is used and equally for more complex attribute creation you can use the
<b>WriteStartAttribute</b>/<b>WriteEndAttribute</b> pair. The code sample below creates an XML file with a single book.</div>
writer.WriteComment("This file represents another fragment of a book store inventory database");
writer.WriteStartElement("bookstore");
writer.WriteStartElement("book", null);
writer.WriteAttribute("genre","autobiography");
writer.WriteAttribute("publicationdate","1979");
writer.WriteAttribute("ISBN","0-7356-0562-9");
writer.WriteElementString("title", null, "The Autobiography of Mark Twain");
writer.WriteStartElement("Author", null);
writer.WriteElementString("first-name", "Mark");
writer.WriteElementString("last-name", "Twain");
writer.WriteEndElement();
writer.WriteElementString("price", "7.99");
writer.WriteEndElement();
writer.WriteEndElement();
//Write the XML to file and close the writer
writer.Flush();
writer.Close();
</xmp></div>
<div style="width:660">
First the XML declaration with the version "1.0" is written with the <b>WriteXmlDecl</b> method.
This method, although optional, must be the first write call before any others writes, otherwise an exception is thrown.
Next the document type is written with the name "bookstore" with the method <b>WriteDocType</b>. The 3rd parameter writes SYSTEM "books.dtd" to indicate that there is
an external DTD to validate against. The <b>Formatting</b> property sets the output to be formatted. This causes child elements to be indented using the <b>Indentation</b>
and <b>IndentChar</b> properties. The <b>Flush</b> method persists the XML to file (here only the <b>Close</b> method is really required, however there are occassions
whereby the XML generated so far needs to be persisted and the writer reused)
</div>
<br clear="left"><div style="width:660">
To check the output from the XmlTextWriter, we can perform a round trip test by reading in the generated file with an XmlTextReader to validate that the XML
is well formed.
<H4>Summary</H4>
<OL>
<LI>The XmlTextWriter provides a fast, forward only way of generating XML.
<LI>The XmlTextWriter helps you to write XML documents that conform to the <A href="http://www.w3.org/TR/1998/REC-xml-19980210">W3C Extensible Markup
Language (XML) 1.0</A></A> specification and the <A><A href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</A></A> specification.
<LI>The XmlTextWriter provides constructors to write XML to a file, a stream or a TextWriter.
<LI>For each XML type there are corresponding XML write methods.