Saturday, February 21, 2009

Create XML file using C#

Namespaces

using System.Data;
using System.Drawing;
using System.Xml;
using System.IO;

//code

XmlTextWriter xmlWriter = new XmlTextWriter("temp.xml", System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("root");
xmlWriter.Close();
// creation complete

doc = new XmlDocument();
doc.Load("temp.xml");
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "firstnode", ""); // creating Node
newNode.InnerText = “Content”; // set your content
doc.DocumentElement.AppendChild(newNode); //Adds a Node
doc.Save("temp.xml");

No comments:

Post a Comment