A.Gecu A.Gecu - 1 year ago 101
C# Question

How to update xml file in c#

the XML file that i have


when i click button_2 in c#, the value in textbox, will make money in xml to update.

the c# codes that i have

XDocument doc = XDocument.Load(@"XMLFile1.xml");
var node = doc.Descendants("Account").FirstOrDefault(personDetail => personDetail.Element("money").Value==txt.Text);
node.SetElementValue("money", this.txt.Text);

Answer Source

Use XDocument to parse the xml string. Find the money XElement and update his value.

public void Button1_Click(object sender, EventArgs e)
    XDocument doc = XDocument.Load(@"XMLFile1.xml");

    var result = doc.Root.Descendants("money").FirstOrDefault();

    if (result == null)

    result.Value = txt.Text;



Check dotNetFiddle