How To Write XML to a File in Visual Studio .NET (2002) Professional Edition (301282)
The information in this article applies to:
- Microsoft Visual Studio .NET (2002), Professional Edition
This article was previously published under Q301282 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 "raw" 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 ("newbooks.xml", System.Text.Encoding.UTF8)
C#
XmlTextWriter myXmlTextWriter = new XmlTextWriter ("newbooks.xml", 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);
- Write comments as necessary by using the WriteComment method:
Visual Basic
myXmlTextWriter.WriteComment("This is a comment")
C#
myXmlTextWriter.WriteComment("This is a comment");
- 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("bookstore")
myXmlTextWriter.WriteStartElement("book")
'Create an element named 'title' with a text node.
' and then close the element
myXmlTextWriter.WriteStartElement("title")
myXmlTextWriter.WriteString("The Autobiography of Mark Twain")
myXmlTextWriter.WriteEndElement ()
'Create an element named 'Author'.
myXmlTextWriter.WriteStartElement("Author")
'Create an element named 'first-name' with a text node
' and close it in one line.
myXmlTextWriter.WriteElementString("first-name", "Mark")
'Create an element named 'first-name' with a text node.
myXmlTextWriter.WriteElementString("last-name", "Twain")
'Close off the parent element.
myXmlTextWriter.WriteEndElement()
'Create an element named 'price' with a text node
' and close it in one line.
myXmlTextWriter.WriteElementString("price", "7.99")
'Close off all elements.
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteEndElement()
C#
myXmlTextWriter.WriteStartElement("bookstore");
myXmlTextWriter.WriteStartElement("book", null);
myXmlTextWriter.WriteElementString("title", null, "The Autobiography of Mark Twain");
myXmlTextWriter.WriteStartElement("Author", null);
myXmlTextWriter.WriteElementString("first-name", "Mark");
myXmlTextWriter.WriteElementString("last-name", "Twain");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteElementString("price", "7.99");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();
- 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("book")
myXmlTextWriter.WriteAttributeString("genre","autobiography")
myXmlTextWriter.WriteAttributeString("publicationdate","1979")
myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9")
myXmlTextWriter.WriteEndElement()
C#
myXmlTextWriter.WriteStartElement("book", null);
myXmlTextWriter.WriteAttributeString("genre","autobiography");
myXmlTextWriter.WriteAttributeString("publicationdate","1979");
myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9");
myXmlTextWriter.WriteEndElement();
- Optionally use the Flush method to persist the XML to a file:Visual Basic
myXmlTextWriter.Flush()
C#
myXmlTextWriter.Flush();
- Use the Close method to persist the XML to a file and close the file:
Visual Basic
myXmlTextWriter.Close()
C#
myXmlTextWriter.Close();
- 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("newbooks.xml", System.Text.Encoding.UTF8)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartDocument(False)
myXmlTextWriter.WriteComment("This is a comment")
'Create the main document element.
myXmlTextWriter.WriteStartElement("bookstore")
myXmlTextWriter.WriteStartElement("book")
'Create an element named 'title' with a text node
' and then close the element.
myXmlTextWriter.WriteStartElement("title")
myXmlTextWriter.WriteString("The Autobiography of Mark Twain")
myXmlTextWriter.WriteEndElement()
'Create an element named 'Author'.
myXmlTextWriter.WriteStartElement("Author")
'Create an element named 'first-name' with a text node
' and close it in one line.
myXmlTextWriter.WriteElementString("first-name", "Mark")
'Create an element named 'first-name' with a text node.
myXmlTextWriter.WriteElementString("last-name", "Twain")
'Close off the parent element.
myXmlTextWriter.WriteEndElement()
'Create an element named 'price' with a text node
' and close it in one line.
myXmlTextWriter.WriteElementString("price", "7.99")
'Close off the book element.
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteStartElement("book")
myXmlTextWriter.WriteAttributeString("genre", "autobiography")
myXmlTextWriter.WriteAttributeString("publicationdate", "1979")
myXmlTextWriter.WriteAttributeString("ISBN", "0-7356-0562-9")
'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>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
XmlTextWriter myXmlTextWriter = new XmlTextWriter ("newbooks.xml", null);
myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument(false);
myXmlTextWriter.WriteComment("This is a comment");
myXmlTextWriter.WriteStartElement("bookstore");
myXmlTextWriter.WriteStartElement("book", null);
myXmlTextWriter.WriteElementString("title", null, "The Autobiography of Mark Twain");
myXmlTextWriter.WriteStartElement("Author", null);
myXmlTextWriter.WriteElementString("first-name", "Mark");
myXmlTextWriter.WriteElementString("last-name", "Twain");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteElementString("price", "7.99");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.Flush();
myXmlTextWriter.WriteStartElement("book", null);
myXmlTextWriter.WriteAttributeString("genre","autobiography");
myXmlTextWriter.WriteAttributeString("publicationdate","1979");
myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.Flush();
myXmlTextWriter.Close();
Console.ReadLine();
}
}
}
- Build and then run your project.
back to the top
REFERENCES
XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation (available from MSDN Magazine):
XmlWriter Class (Microsoft .NET Framework Class Library)
Features of XMLWriter (Microsoft .NET Framework Developer's Guide)
back to the top
Modification Type: | Major | Last Reviewed: | 4/18/2006 |
---|
Keywords: | kbhowto kbHOWTOmaster KB301282 kbAudDeveloper |
---|
|