I am making a program that takes information from the user and saves it as an XML file. The information includes a name, and other details. I would like to be able to allow the user to enter the same name, and instead of a new element being created with the same name, it would overwrite the existing element with the new values entered.
I hope that made sense, but in case not an example would be if I entered the name John Smith, and age 250. Clearly I would like to change the typo, so I would do it again to enter John Smith, age 25. When I do this a new element is created to add another John Smith, is there a way to just edit/overwrite the existing element? I'll post the code I use to create the XML if it is any help!
Thanks in advance!
XDocument Xdoc = new XDocument(new XElement("XMLFile"));
if (System.IO.File.Exists(filepath))
{
Xdoc = XDocument.Load(filepath);
}
else
{
Xdoc = new XDocument();
}
XElement xml = new XElement("Member");
xml.Add(new XElement("Name", txtName.Text));
xml.Add(new XElement("Age", txtAge.Text));
xml.Add(new XElement("Nationality", txtNationality.Text));
xml.Add(new XElement("EmailAddress", txtEmailAddress.Text));
xml.Add(new XElement("ContactNumber", txtContactNumber.Text));
if (Xdoc.Descendants().Count() > 0)
{
Xdoc.Descendants().First().Add(xml);
}
else
{
Xdoc.Add(xml);
}
Xdoc.Save(filepath);