The XmlWriter and XmlTextWriter classes are defined in the System.XML namespace.
The XmlTextWriter class is derived from XmlWriter class, which represents a writer that provides fast non-cached forward-only way of generating XML documents based on the W3C Extensible Markup Language (XML) 1.0 specification.
In this article, I will show you how to use XmlTextWriter class to create an XML document and write data to the document.
Adding namespace Reference
Since Xml classes are defined in the System.XML namespace, so first thing you need to do is to Add the System.XML reference to the project.
using System.Xml;
Creating an XML Document
The constructor of the XmlTextWriter class creates an XML file if file doesn't exist. In this sample, I create a new XML file called xmltest.xml in C\temp directory.
XmlTextWriter writer = new XmlTextWriter("C:\\temp\\xmltest.xml", null);
NOTE: If you don't want to write data in an XML file and want to display XML contents on the Console, pass Console.Out as a parameter of the constructor.
XmlTextWriter writer = new XmlTextWriter(Console.Out);
Adding Data to the Document
The WriteStartDocument method starts a new document. The WriteStartElement and the WriteEndElement pair is used to add a new element to the document. The WriteString writes a string to the document.
writer.WriteStartDocument();
writer.WriteComment("Commentss: XmlWriter Test Program");
writer.WriteProcessingInstruction("Instruction","Person Record");
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("LastName","");
writer.WriteString("Chand");
writer.WriteEndElement();
writer.WriteElementInt16("age","", 25);
writer.WriteEndDocument();
Souce Code: Attachment createxml1.cs 2 KB
namespace WriteToXML
{
using System;
using System.Xml;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
}
public static int Main(string[] args)
{
try
{
// Creates an XML file is not exist
XmlTextWriter writer = new XmlTextWriter("C:\\temp\\xmltest.xml", null);
// Starts a new document
writer.WriteStartDocument();
//Write comments
writer.WriteComment("Commentss: XmlWriter Test Program");
writer.WriteProcessingInstruction("Instruction","Person Record");
// Add elements to the file
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("LastName","");
writer.WriteString("Chand");
writer.WriteEndElement();
writer.WriteStartElement("FirstName","");
writer.WriteString("Mahesh");
writer.WriteEndElement();
writer.WriteElementInt16("age","", 25);
// Ends the document
writer.WriteEndDocument();
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
return 0;
}
}
}

mohamed shiraziPosted Apr 9, 2011, 10:52 PM
the code above writes xml elements and saves into "C:\\temp\\xmltest.xml". if use the same code to enter a new element, it will replace the previous elements. Now i would like to add a new element to this file along with the previous elements. That is, the new element should append to the elements list of "C:\\temp\\xmltest.xml".
Koteswararao MallisettiPosted Sep 4, 2010, 5:48 AM
IT JUST CREATE THE FILE WITH OUT ANY DATA
Rajesh KumarPosted May 11, 2010, 4:33 AM
Sir this is Rajesh, I want to write xml file using xmlTextWriter. but this will not support to write text with bold, italic tag, like <b>Rajesh </b>, this is will generate the xml file. this is not in proper format. i use the code like. xw.WriteStartElement("Artilce"); xw.WriteString(textBox2.Text); or tw.WriteElementString("Name",textBox2.Text); xw.WriteEndDocument(); what i m using for. please give some suggestion.
Hari NidamarthyPosted Apr 13, 2010, 8:29 AM
writer.Close() statement is missing
vimal kumarPosted Mar 29, 2010, 5:59 AM
zczxzx
nazrin sPosted May 25, 2009, 8:01 AM
i used your code for generating an xml.now an xml is created currectly using your code,but cannot add data to that xml.that is adding data to document part of this code is not working well.so will you please give me a clear solution..
ravi kiranPosted Dec 18, 2007, 12:36 PM
can I add new elements for the existing xml file? If so please let me know the solution steps. Reards, Kiran
Jacques Du PreezPosted Jan 13, 2006, 1:06 AM
I'm using the xmltextwriter class to create a xml file, then i view the file as html using xsl stylesheet. My question being, how do i insert the tag <?xml:stylesheet type="text/xsl" href=""?> using the xmltextwriter Thanx in advance.