Thursday 9 February 2012

XML FILE OPERATIONS : CREATE AND INSERT IN XML FILE



protected void InsertXML(string xmlFilePath, string elements, string element)
{
 if (!File.Exists(xmlFilePath))
{
string data = "<" + elements + ">" + "</" + elements + ">";
doc.Load(new StringReader(data));
XmlElement child = doc.CreateElement(element);
child.SetAttribute("NAME", TextBox1.Text);
child.SetAttribute("CITY", TextBox2.Text);
child.SetAttribute("NUMBER", TextBox3.Text);
doc.DocumentElement.AppendChild(child);
doc.Save(xmlFilePath);
         
}
else
{
doc.Load(xmlFilePath);
XmlElement child = doc.CreateElement(element);
child.SetAttribute("NAME", TextBox1.Text);
child.SetAttribute("CITY", TextBox2.Text);
child.SetAttribute("NUMBER", TextBox3.Text);
doc.DocumentElement.AppendChild(child);
doc.Save(xmlFilePath);
}
            }



No comments:

Post a Comment