如何使用c#代码搜索xml文件并替换某个值
在这种情况下 我想改变
version="5.25"
到
version="6.25"
答案 0 :(得分:1)
使用linq to xml:
var doc = XDocument.Parse(yourXMLGoesHere);
var elementsWithVersionAttribute) = doc.Descendants()
.Where(e => e.Attribute("version")!=null)
.Where(e => e.Attribute("version").Value == "5.25");
foreach(var element in elementsWithVersionAttribute)
{
element.SetAttributeValue("version", "6.25");
}
你可能会让上面的代码变得更短 - 但希望跟循环中的查询更容易。