本文介绍了有效删除c#中重复的xml元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个包含大量重复条目的 XML 文件,例如这些.

I have couple of XML files that contain lots of duplicate entries, such as these.

<annotations>
  <annotation value=",Clear,Outdoors" eventID="2">
    <image location="Location 1" />
    <image location="Location 2" />
    <image location="Location 2" />
  </annotation>

  <annotation value=",Not a problem,Gravel,Shopping" eventID="2">
    <image location="Location 3" />
    <image location="Location 4" />
    <image location="Location 5" />
    <image location="Location 5" />
    <image location="Location 5" />
  </annotation>
</annotations>

我想删除每个子元素中的重复元素.我解决这个问题的方法是将所有元素复制到一个列表中,然后比较它们,

I want to remove the duplicate elements in the each of the child. The way I approached this is by copying all the elements to a list and then comparing them,

 foreach (var el in xdoc.Descendants("annotation").ToList())
   {
      foreach (var x in el.Elements("image").Attributes("location").ToList())
       {
           //add elements to a list
       }
   }

中途我意识到这是非常低效和耗时的.我对 XML 还很陌生,我想知道 C# 中是否有任何内置方法可以用来删除重复项?

half way through I realized this is very inefficient and time consuming. I'm fairly new to XML, I was wondering if there are any built in methods in C# that I can use to remove duplicates?.

我尝试使用

if(!x.value.Distinct()) // can't convert collections to bool
    x.Remove();

但这行不通,也不行

if(x.value.count() > 1) // value.count returns the number of elements.
   x.Remove()

推荐答案

using System.Xml.Linq;

XDocument xDoc = XDocument.Parse(xmlString);
xDoc.Root.Elements("annotation")
         .SelectMany(s => s.Elements("image")
                           .GroupBy(g => g.Attribute("location").Value)
                           .SelectMany(m => m.Skip(1))).Remove();

这篇关于有效删除c#中重复的xml元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 11:54