Reputation: 27
I'm trying to edit a record inside a XML document, using C#, with the following structure:
<?xml version="1.0" standalone="yes"?>
<Questionnaire>
<Record>
<Pile>1</Pile>
<Serno>1</Serno>
</Record>
</Questionnaire>
I'm selecting the record successfully by using:
SernoID=txt_Serno.Text;
XElement xml = XElement.Load("path");
XElement x = xml.Elements().Where(p => p.Element("Serno").Value.Equals(sernoID.ToString())).First();
Now, if the user changes any data in the winform, I want to update it into the xml file, for which I'm using:
x.Element("Pile").Value = txt_pile.Text;
x.Save("path");
However, it keeps only the modified record, and removes all the other existing records. It probably has something to do with the save command?
Can anyone help please?
Thanks!!
Upvotes: 0
Views: 55
Reputation: 26213
x
is the result of your query, so it is a single Record
element. You then save that to your file at the specified path. This has the effect of overwriting your file with just this single element.
Make your changes as before, but change this line:
x.Save("path");
To this:
xml.Save("path");
This will save the whole document, not just a single element.
Upvotes: 2