我写console application
程序这是我的xml file
:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<AsteriskHost type="string">172.16.18.14</AsteriskHost>
</Settings>
我运行这段代码
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");
}
这是我的测试:
[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);
}
我没有任何错误,但我的新值没有保存在文件中。
如何xml node
在 C# 中更新