简体   繁体   中英

How can i update xml file in c#

I write console application program this is my xml file :

    <?xml version="1.0" encoding="utf-8" ?>
    <Settings>
      <AsteriskHost type="string">172.16.18.14</AsteriskHost>

</Settings>

I run this code

   public void Set(List<AcmSettings> acmSettings)
    {
        XElement xelement = XElement.Load("Settings.xml");
        IEnumerable<XElement> settings = xelement.Elements();
        foreach (var item in acmSettings)
        {
            settings.FirstOrDefault(x => x.Name == item.Name).SetValue("treeee");
        }
        xelement.Save("Settings.xml");
    }

this my test:

[Test]
public void SetShouldUpdateValue()
{
    var settingsManager = new SettingsManager();
    const string newIp = "165.166.167.167";
    const string elemntName = "AsteriskHost";
    var acmSetting = new List<AcmSettings> { new AcmSettings { Name = elemntName, Value = newIp } };
    settingsManager.Set(acmSetting);
    var setting = settingsManager.Get(x => x.Name == elemntName).FirstOrDefault();
    Assert.IsTrue(setting != null);
    Assert.IsTrue(setting.Value== newIp);

}

I don't have any error but my new value not save in file.

How can I update xml node in c#

I tried your Set method, and it runs ok for me. I did have to tweak your current implementation by changing just one line:

// settings.FirstOrDefault(x => x.Name == item.Name).SetValue("treeee");
settings.FirstOrDefault(x => x.Name == item.Name).SetValue(item.Value);

Perhaps the problem is with your Get method. Here is a quick (not production quality) Get implementation. Using this implementation, your unit test runs successfully.

// note - just a string (name) passed in
public XElement Get(string name)
{
    XElement xelement = XElement.Load("Settings.xml");
    IEnumerable<XElement> settings = xelement.Elements();

    return settings.FirstOrDefault(x => x.Name == name);
}

The other possibility is that, as suggested in the comments above, is that you are looking at your project XML file, and not looking at the output XML file.

Try like this

 XmlDocument xmlDom = new XmlDocument();
 xmlDom.Load("YourXMLFILEPATH.xml");
 XmlNode newXMLNode = xmlDom.SelectSingleNode("/Settings/AsteriskHost");
 newXMLNode.InnerText = YourValue;
 xmlDom.Save("YourXMLFILEPATH.xml");
 Console.WriteLine(xmlDom);

Have you tried this ?

You can modify your set method like this.

    public void Set(List<AcmSettings> acmSettings)
    {
        XElement xelement = XElement.Load("Settings.xml");
        IEnumerable<XElement> settings = xelement.Elements();
        foreach (var item in acmSettings)
        {
            xelement.Descendants(item.Name).FirstOrDefault().Value = item.Value;
        }
        xelement.Save("Settings.xml");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM