May 13, 2008

Appending Data to Xml File

// path is the directory where XML file exists

if (!File.Exists(path))
{
XmlTextWriter xt = new XmlTextWriter(path,null);
xt.WriteStartDocument();
xt.WriteStartElement("StartElementName");
xt.WriteEndDocument();
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(path);
XmlElement xe = xmldoc.CreateElement("ElementName");
xe.SetAttribute("AttributeName", textBox2.Text.Trim());
xmldoc.DocumentElement.AppendChild(xe);
xmldoc.Save(path);

No comments: