Click here to Skip to main content
16,022,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void butAdd_Click(object sender, EventArgs e)
   {
       try
       {
           XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));

           xmlDoc.Element("Persons").Add(new XElement("Person", new XElement("Name", txtName.Text),
           new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));

           xmlDoc.Save(Server.MapPath("People.xml"));
           jsCall("Data successfully added to XML file.");
           readXML();
       }
       catch
       {
           jsCall("Sorry, unable to process request. Please try again.");
       }
   }
Posted
Comments
sureshsankar 18-Jul-13 7:53am    
help me friends

1 solution

The file People.xml which the server loaded does not contain a Persons node.
So, check first: if (xmlDoc.Element("Persons") == null) { then create that node and add it to the document; }
 
Share this answer
 
Comments
sureshsankar 19-Jul-13 0:15am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900