我怎样才能改变在C#中的XML文件的属性值?

问题描述:

我有一个XML文件(Web.config),我需要编辑每个标签的价值属性,取决于关键的名字......

I have a XML file(web.config) and I need to edit the value attribute of each tag, depend of the key name...

这是XML文件的例子:

this is an example of the XML file:

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

我的意思是,我怎样才能改变价值喜和放大器; 你好使用的关键属性(A1&安培; B1)??

I mean, How can I change the value "hi" & "hello" using the key attribute(A1 & B1) ??

非常感谢

试试这个code,它工作正常:

try this code, it works fine:

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";
}