Click here to Skip to main content
15,066,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello , I've been trying to change XML node's innertext from a WinForm by using C# .
MessageBox shows the "needed".
I've got no idea why It is not changing the xml file.
It is not giving any exceptions.



C#
private void btnUpdate_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"users.xml");
            XmlNodeList elem = doc.GetElementsByTagName("ID");
            foreach (XmlNode gettags in elem)
            {
                gettags.FirstChild.InnerText = "needed";
             gettags.FirstChild.Value="needed";  
                MessageBox.Show(gettags.FirstChild.InnerText);
            }
        }
Posted

1 solution

If you change a value, it doesn't save the file automatically. If you're ready updating your file, save it using the Save method:
C#
private void btnUpdate_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"users.xml");
            XmlNodeList elem = doc.GetElementsByTagName("ID");
            foreach (XmlNode gettags in elem)
            {
                gettags.FirstChild.InnerText = "needed";
             gettags.FirstChild.Value="needed";  
                MessageBox.Show(gettags.FirstChild.InnerText);
            }
            doc.Save("users.xml"); // <-- save the file
        }
   
v2
Comments
Cenkay Vergili 7-Jul-14 3:13am
   
Thank you so much. How dumb I am.
Thomas Daniels 7-Jul-14 3:18am
   
You're welcome!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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