我有幾個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