Linq to XML:从项目中删除重复项

时间:2014-11-05 16:26:55

标签: c# .net xml linq-to-xml

我有一个包含以下内容的xml文件:

 <item>
  <key>Some text</key>
  <explanation>Some more text</explanation>
  <content language="en-gb">word</content>
  <content language="en">word</content>
</item>

我想查看具有<content>属性的language个标记是否具有相同的值,然后删除重复的字段。在这种情况下,例如,有两个content标记,其值为word。我想删除其中一个。

2 个答案:

答案 0 :(得分:2)

您可以使用简单的LINQ to XML查询执行此操作:

var duplicates = xml.Descendants("content")
        .GroupBy(g => (string)g.Value)
        .Where(g => g.Count() > 1)
        .SelectMany(g => g.Take(1));

duplicates.Remove();

答案 1 :(得分:1)

var contents = XDocument.Parse(xml);

// Select only elements that have the language attribute
var result = from item in contents.Descendants()
             where item.Attribute("language") != null
             select item;

// Returns only those elements that have at least another element
// with the same value.
var resultDuplicates = result
    .GroupBy(s => s.Value)
    .SelectMany(grp => grp.Skip(1));

// If duplicates found, replace them in the original xml.
if (resultDuplicates.Count() > 0)
{
    foreach(var entry in resultDuplicates)
        xml = xml.Replace(entry.ToString(), string.Empty);
}