我有几个XML文件包含大量重复的条目,如这些。高效地删除C#中的重复XML元素
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>
我想删除每个孩子中的重复元素。我走近这个问题的方法是通过复制的所有元素的列表,然后对它们进行比较,
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#中的内置方法可用于删除重复项?
我尝试使用
if(!x.value.Distinct()) // can't convert collections to bool
x.Remove();
但是,这并不工作,同样没有
退房'GroupBy()' – 2014-09-12 16:18:34