Microsoft KB Archive/301282

= How To Write XML to a File in Visual Studio .NET (2002) Professional Edition =

Article ID: 301282

Article Last Modified on 4/18/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition

-



This article was previously published under Q301282





IN THIS TASK
SUMMARY
 * Requirements
 * Write XML to a File

REFERENCES



SUMMARY
This article shows how to write XML to a file by using the XmlTextWriter class. XmlTextWriter provides a fast, forward-only way of generating XML and helps you to build XML documents. XmlTextWriter provides stream write rather than using an object model, such as the XML Document Object Model (DOM), and so gives better performance.

back to the top

Requirements
The following items describe the recommended hardware, software, network infrastructure, and service packs that you will need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * XML terminology
 * Creating and reading an XML file

back to the top

Write XML to a File
 Open Visual Studio .NET. Create a new Microsoft Visual Basic or Microsoft C# console application. You can jump directly to step 12 for a complete code listing. More detailed steps are provided here.  Make sure that the System.XML namespace is referenced by the project. Use the Imports statement on the System.XML namespace so you will not be required to qualify XMLTextReader declarations later in your code. The Imports statement must be used prior to any other declarations:Visual Basic Imports System.Xml C# using System.Xml;   Construct an XmlTextWriter. Typically the XmlTextWriter is used if you want to write XML as &quot;raw&quot; data without the overhead of a DOM, and therefore it provides a faster mechanism for writing XML. The XmlTextWriter has different constructors to specify the location to write the XML data to. Next, you will write XML to the Newbooks.xml file. Add the code to construct the XmlTextWriter in the main procedure in the main module:

Visual Basic Dim myXmlTextWriter As XmlTextWriter = new XmlTextWriter (&quot;newbooks.xml&quot;, System.Text.Encoding.UTF8) C# XmlTextWriter myXmlTextWriter = new XmlTextWriter (&quot;newbooks.xml&quot;, System.Text.Encoding.UTF8); The constructor takes the file name that you want to write to and the encoding that you would like to generate.   Specify how you would like the XML file to be formatted by using the Formatting property. This causes child elements to be indented through use of the Indentation and IndentChar properties:Visual Basic myXmlTextWriter.Formatting = System.Xml.Formatting.Indented C# myXmlTextWriter.Formatting = Formatting.Indented;   Start the XML file with the XML declaration by using the WriteStartDocument method:Visual Basic myXmlTextWriter.WriteStartDocument(false) C# myXmlTextWriter.WriteStartDocument(false); </li>  Write comments as necessary by using the WriteComment method: Visual Basic myXmlTextWriter.WriteComment(&quot;This is a comment&quot;) C# myXmlTextWriter.WriteComment(&quot;This is a comment&quot;); </li>  Use the WriteStartElement, WriteEndElement, WriteString, and WriteElementString methods to create XML element nodes and text nodes. The WriteElementString method will start the element, write the string given in as an argument (if any), and close the element in one line.Visual Basic 'Create the main document element. myXmlTextWriter.WriteStartElement(&quot;bookstore&quot;) myXmlTextWriter.WriteStartElement(&quot;book&quot;)

'Create an element named 'title' with a text node. ' and then close the element myXmlTextWriter.WriteStartElement(&quot;title&quot;) myXmlTextWriter.WriteString(&quot;The Autobiography of Mark Twain&quot;) myXmlTextWriter.WriteEndElement

'Create an element named 'Author'. myXmlTextWriter.WriteStartElement(&quot;Author&quot;)

'Create an element named 'first-name' with a text node ' and close it in one line. myXmlTextWriter.WriteElementString(&quot;first-name&quot;, &quot;Mark&quot;)

'Create an element named 'first-name' with a text node. myXmlTextWriter.WriteElementString(&quot;last-name&quot;, &quot;Twain&quot;)

'Close off the parent element. myXmlTextWriter.WriteEndElement

'Create an element named 'price' with a text node ' and close it in one line. myXmlTextWriter.WriteElementString(&quot;price&quot;, &quot;7.99&quot;)

'Close off all elements. myXmlTextWriter.WriteEndElement myXmlTextWriter.WriteEndElement

C# myXmlTextWriter.WriteStartElement(&quot;bookstore&quot;); myXmlTextWriter.WriteStartElement(&quot;book&quot;, null); myXmlTextWriter.WriteElementString(&quot;title&quot;, null, &quot;The Autobiography of Mark Twain&quot;); myXmlTextWriter.WriteStartElement(&quot;Author&quot;, null); myXmlTextWriter.WriteElementString(&quot;first-name&quot;, &quot;Mark&quot;); myXmlTextWriter.WriteElementString(&quot;last-name&quot;, &quot;Twain&quot;); myXmlTextWriter.WriteEndElement; myXmlTextWriter.WriteElementString(&quot;price&quot;, &quot;7.99&quot;); myXmlTextWriter.WriteEndElement; myXmlTextWriter.WriteEndElement;

</li>  You can use the WriteAttributeString method to create XML attributes. The method takes the name of the attribute and its value as arguments:Visual Basic myXmlTextWriter.WriteStartElement(&quot;book&quot;) myXmlTextWriter.WriteAttributeString(&quot;genre&quot;,&quot;autobiography&quot;) myXmlTextWriter.WriteAttributeString(&quot;publicationdate&quot;,&quot;1979&quot;) myXmlTextWriter.WriteAttributeString(&quot;ISBN&quot;,&quot;0-7356-0562-9&quot;) myXmlTextWriter.WriteEndElement C# myXmlTextWriter.WriteStartElement(&quot;book&quot;, null); myXmlTextWriter.WriteAttributeString(&quot;genre&quot;,&quot;autobiography&quot;); myXmlTextWriter.WriteAttributeString(&quot;publicationdate&quot;,&quot;1979&quot;); myXmlTextWriter.WriteAttributeString(&quot;ISBN&quot;,&quot;0-7356-0562-9&quot;); myXmlTextWriter.WriteEndElement; </li>  Optionally use the Flush method to persist the XML to a file:Visual Basic myXmlTextWriter.Flush C# myXmlTextWriter.Flush; </li>  Use the Close method to persist the XML to a file and close the file: Visual Basic myXmlTextWriter.Close C# myXmlTextWriter.Close; </li>  Here is a complete code listing for your convenience:

Visual Basic 'Visual Basic .Net Imports System.Xml Module Module1

Sub Main

Dim myXmlTextWriter As XmlTextWriter = New XmlTextWriter(&quot;newbooks.xml&quot;, System.Text.Encoding.UTF8) myXmlTextWriter.Formatting = System.Xml.Formatting.Indented myXmlTextWriter.WriteStartDocument(False) myXmlTextWriter.WriteComment(&quot;This is a comment&quot;) 'Create the main document element. myXmlTextWriter.WriteStartElement(&quot;bookstore&quot;) myXmlTextWriter.WriteStartElement(&quot;book&quot;)

'Create an element named 'title' with a text node ' and then close the element. myXmlTextWriter.WriteStartElement(&quot;title&quot;) myXmlTextWriter.WriteString(&quot;The Autobiography of Mark Twain&quot;) myXmlTextWriter.WriteEndElement

'Create an element named 'Author'. myXmlTextWriter.WriteStartElement(&quot;Author&quot;)

'Create an element named 'first-name' with a text node ' and close it in one line. myXmlTextWriter.WriteElementString(&quot;first-name&quot;, &quot;Mark&quot;)

'Create an element named 'first-name' with a text node. myXmlTextWriter.WriteElementString(&quot;last-name&quot;, &quot;Twain&quot;)

'Close off the parent element. myXmlTextWriter.WriteEndElement

'Create an element named 'price' with a text node ' and close it in one line. myXmlTextWriter.WriteElementString(&quot;price&quot;, &quot;7.99&quot;)

'Close off the book element. myXmlTextWriter.WriteEndElement

myXmlTextWriter.WriteStartElement(&quot;book&quot;) myXmlTextWriter.WriteAttributeString(&quot;genre&quot;, &quot;autobiography&quot;) myXmlTextWriter.WriteAttributeString(&quot;publicationdate&quot;, &quot;1979&quot;) myXmlTextWriter.WriteAttributeString(&quot;ISBN&quot;, &quot;0-7356-0562-9&quot;) 'Close off the book element. myXmlTextWriter.WriteEndElement 'Close off the Parent Element bookstore. myXmlTextWriter.WriteEndElement

myXmlTextWriter.Flush myXmlTextWriter.Close 'Waits for user to press enter before exiting the program. Console.ReadLine

End Sub

End Module

C# //C# using System; using System.Xml;

namespace WriteXMLtoFile {   ///     /// Summary description for Class1. ///    class Class1 {       static void Main(string[] args) {           XmlTextWriter myXmlTextWriter = new XmlTextWriter (&quot;newbooks.xml&quot;, null); myXmlTextWriter.Formatting = Formatting.Indented; myXmlTextWriter.WriteStartDocument(false); myXmlTextWriter.WriteComment(&quot;This is a comment&quot;); myXmlTextWriter.WriteStartElement(&quot;bookstore&quot;); myXmlTextWriter.WriteStartElement(&quot;book&quot;, null); myXmlTextWriter.WriteElementString(&quot;title&quot;, null, &quot;The Autobiography of Mark Twain&quot;); myXmlTextWriter.WriteStartElement(&quot;Author&quot;, null); myXmlTextWriter.WriteElementString(&quot;first-name&quot;, &quot;Mark&quot;); myXmlTextWriter.WriteElementString(&quot;last-name&quot;, &quot;Twain&quot;); myXmlTextWriter.WriteEndElement; myXmlTextWriter.WriteElementString(&quot;price&quot;, &quot;7.99&quot;); myXmlTextWriter.WriteEndElement; myXmlTextWriter.Flush; myXmlTextWriter.WriteStartElement(&quot;book&quot;, null); myXmlTextWriter.WriteAttributeString(&quot;genre&quot;,&quot;autobiography&quot;); myXmlTextWriter.WriteAttributeString(&quot;publicationdate&quot;,&quot;1979&quot;); myXmlTextWriter.WriteAttributeString(&quot;ISBN&quot;,&quot;0-7356-0562-9&quot;); myXmlTextWriter.WriteEndElement; myXmlTextWriter.WriteEndElement;

myXmlTextWriter.Flush; myXmlTextWriter.Close; Console.ReadLine;

}   } }

</li> Build and then run your project.</li></ol>

back to the top

<div class="references_section">