我試圖更新這個小的XML示例文件沒有成功。 它沒有給我任何錯誤,但文件保持不變。 任何想法?更新C#中的現有XML文件
感謝
XML示例:
<test>
<user>John Doe</user>
<user>Jane Doe</user>
</test>
C#代碼
XmlDocument doc = new XmlDocument();
doc.Load("../../../test.xml");
XmlNode sNode = doc.SelectSingleNode("/test/user");
XmlAttribute users = sNode.Attributes["user"];
if (users != null)
{
string currentValue = users.Value;
if (string.IsNullOrEmpty(currentValue))
{
users.Value = "Thomas";
}
}
doc.Save("../../../test1.xml");
'sNode.Attributes [「我們呃「]'你的xml不包含用戶屬性 – DanielS