This tutorial will show you how to write xml documents with C#.NET using the XmlTextWriter class from the System.Xml namespace. To view the article on how to read xml documents using C#.NET, click here

Note: Remember to set the “write” permissions for your webserver on the directory where you like to write the XML document to.

The XML classes in the System.Xml namespace that can be used to write XML documents are:
  • XmlWriter
  • XmlTextWriter

XmlTextWriter is inherited from XmlWriter which means (in short) XmlWriter has a couple of extra features. But for the purpose of this tutorial and in most cases, you’ll only need to use XmlTextWriter.

I’ve added comments to the lines of code below to explain what they do:

Code:
<%@ Page Language="C#" %>
<%@ import Namespace="System.Xml" %>
<script runat="server">
public void Page_Load(object Src, EventArgs E)
{

	//*** Written by user CWA, CoolWebAwards.com Forums. 18 March 2010
	//*** http://forum.coolwebawards.com/
	
	// *** XmlTextWriter
	XmlTextWriter myXmlWriter = new XmlTextWriter(Server.MapPath("test2.xml"), null);
	myXmlWriter.Formatting = Formatting.Indented;
	myXmlWriter.Indentation = 4;
	
	// *** Uncomment to use XmlWriter for extra settings if necessary. Comment the 3 lines above
	//Dim myXmlSettings As New XmlWriterSettings
	//myXmlSettings.Indent = True
	//Dim myXmlWriter As XmlWriter = XmlWriter.Create(server.MapPath("test.xml"), myXmlSettings)
	* * 
	myXmlWriter.WriteComment("http://forum.coolwebawards.com");// <!--http://forum.coolwebawards.com-->
	myXmlWriter.WriteStartElement("Products");// <Products>
	
	int i = 0;
	for (i = 1; i <= 3; i++) {
		//http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter_members.aspx
		myXmlWriter.WriteStartElement("Product");// see next line
		myXmlWriter.WriteAttributeString("Id", i.ToString());// <Product id="">
		myXmlWriter.WriteElementString("price", "price element text for ID " + i.ToString());// <price>price element text for ID 1</price>
		myXmlWriter.WriteElementString("weight", "weight element text for ID " + i.ToString());// <weight>price element text for ID 1</price>
		myXmlWriter.WriteRaw(Environment.NewLine + "        <description>description element text for ID " + i.ToString() + "</description>" + Environment.NewLine);// <description>description element text for ID 3</description> | Just an example of how to write raw data
		myXmlWriter.WriteEndElement();//</Product>
	}
	* * * * 
	myXmlWriter.WriteEndElement();// </Products>
	myXmlWriter.Close();

}
</script>
The code above will produce the following xml document called test.xml:

Code:
<!--http://forum.coolwebawards.com-->
<Products>
    <Product Id="1">
        <price>price element text for ID 1</price>
        <weight>weight element text for ID 1</weight>
        <description>description element text for ID 1</description>
	</Product>
    <Product Id="2">
        <price>price element text for ID 2</price>
        <weight>weight element text for ID 2</weight>
        <description>description element text for ID 2</description>
	</Product>
    <Product Id="3">
        <price>price element text for ID 3</price>
        <weight>weight element text for ID 3</weight>
        <description>description element text for ID 3</description>
	</Product>
</Products>
Click here for more information on how to read an xml document using C#.NET