Using XmlTextWriter to write an Xml file
using System;
using System.Xml;
using System.Text;
using System.Xml;
using System.Text;
public class MainClass
{
public static void Main(string [] arg)
{
// To create an Xml file with the following format
// <?xml version="1.0" encoding="Windows-1252" standalone="yes"?>
// <directory>
// <files dir="c:\" />
// </directory>
{
public static void Main(string [] arg)
{
// To create an Xml file with the following format
// <?xml version="1.0" encoding="Windows-1252" standalone="yes"?>
// <directory>
// <files dir="c:\" />
// </directory>
XmlTextWriter xmlText = new XmlTextWriter("filename.xml", Encoding.Default);
xmlText.Formatting = Formatting.Indented;
xmlText.WriteStartDocument(true);
// If you want to attach an Xsl file with the Xml then uncomment the following 2 lines
// It actually adds the xml-stylesheet attribute
// string xslText="type='text/xsl' href='../index.xsl'";
// xmlText.WriteProcessingInstruction("xml-stylesheet", xslText);
// It actually adds the xml-stylesheet attribute
// string xslText="type='text/xsl' href='../index.xsl'";
// xmlText.WriteProcessingInstruction("xml-stylesheet", xslText);
xmlText.WriteStartElement("directory"); // <directory>
xmlText.WriteStartElement("files"); // <files>
xmlText.WriteAttributeString("dir", "c:\\");
xmlText.WriteEndElement(); // </files>
xmlText.WriteEndElement(); // </directory>
xmlText.WriteEndElement(); // </files>
xmlText.WriteEndElement(); // </directory>
xmlText.WriteEndDocument();
xmlText.Close();
}
}
}
}