c# - 如何在 C# 中更改 XML 文件的属性值?

标签 c# .net xml file xmldocument

我有一个 XML 文件(web.config),我需要编辑每个标签的 value 属性,具体取决于键名称...

这是 XML 文件的示例:

<appSettings>
  <add key="A1" value="Hi" />
  <add key="B1" value="Hello" />
</appSettings>

我的意思是,如何使用键属性(A1 和 B1)更改值“hi”和“hello”??

非常感谢

最佳答案

尝试这个代码,它工作正常:

XmlDocument doc = new XmlDocument();
doc.Load("Your.xml");
XmlNodeList elementList = doc.GetElementsByTagName("add");
for (int i = 0; i < elementList.Count; i++)
{
    if(elementList[i].Attributes["key"].Value == "A1")
       elementList[i].Attributes["value"].Value = "NewValue";
}  

关于c# - 如何在 C# 中更改 XML 文件的属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6167543/

相关文章:

javascript - MVC AJAX 查询回调不起作用

c# - 如何使用 C# 获取运行代码的操作系统的名称?

java - 水平居中文本

.net - 如何使用正则表达式进行多行搜索?

c# - 为什么在传递对象(而不是结构)时使用 "ref"?

xml - Groovy XmlSlurper : Find elements in XML structure

xml - 如何在 Windows 上安装 Sxslt 包? R

c# - 为什么 Enumerable.OrderBy<TSource, TKey> 方法在不使用比较器时工作得更快

c# - 重构大 if else 代码

.NET 图表控件 - X 轴文本旋转