Wednesday, July 21, 2010

How to add data to an XML file using LINQ.

Load the XML file.
Specify where and what we want to add.
Save the changes to the XML file.

The method to add new data to the XML file is below.
private void addToXml()
{
XDocument xmlDoc = XDocument.Load("XYZ.xml");

xmlDoc.Element("Players").Add(new XElement("Player", new XElement("Name", txtName.Text),
new XElement("Team", txtTeam.Text), new XElement("Position",cmbPosition.SelectedItem.ToString())));
xmlDoc.Save("XYZ.xml");
}

No comments:

Post a Comment