我怎样才能改变在C#中的XML文件的属性值? [英] How can I change an attribute value of a XML file in c#?
本文介绍了我怎样才能改变在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";
}
这篇关于我怎样才能改变在C#中的XML文件的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文