How to insert the data to the Xml file?

Tripati_tutu
Posted by Tripati_tutu under ASP.NET category on | Points: 40 | Views : 2039
Below is the sample code snippet to insert data to the xml.
protected void InsertRecordToXml_Click(object sender, EventArgs ee)
{
XmlDocument docXml = new XmlDocument();
docXml.Load(Server.MapPath(@"App_Data\TestXmlFile.xml"));

//Create the node to set the data
XmlElement element = docXml.CreateElement("DotNetFunda");
XmlElement xmlUser = docXml.CreateElement("UserName");
XmlElement xmlQuestion = docXml.CreateElement("Question");
XmlElement xmlSolution = docXml.CreateElement("Solution");

xmlUser.InnerText = this.txtUser.Text.Trim();
xmlQuestion.InnerText = this.txtQuestion.Text.Trim();
xmlSolution.InnerText = this.txtSolution.Text.Trim();


element.AppendChild(xmlUser);
element.AppendChild(xmlQuestion);
element.AppendChild(xmlSolution);

docXml.DocumentElement.AppendChild(element);

docXml.Save(Server.MapPath(@"App_Data\TestXmlFile.xml"));

//call the load method to load the data
loadUsersXmlData();
}

Comments or Responses

Login to post response