【问题标题】:Search XML file for a word and update在 XML 文件中搜索单词并更新
【发布时间】:2012-03-03 13:27:45
【问题描述】:

如何使用 c# 代码搜索 xml 文件并替换某个值

在这种情况下 我要改签

version="5.25"

version="6.25"

【问题讨论】:

    标签: c# xml visual-studio


    【解决方案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");
    }
    

    您可能可以使上面的代码更短一些 - 但希望将查询与循环分开来更容易理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多