在XML中查找和删除重复节点的最快方法是什么?

use*_*862 0 .net c# xml linq

XML文件具有这样的结构

<Nodes>
   <Node> one </Node>
   <Node> two </Node>
   <Node> three </Node>
   <Node> three </Node>
</Nodes>
Run Code Online (Sandbox Code Playgroud)

由于xml文件有超过30000个节点,我正在寻找找到和删除重复节点的最快方法.

你会怎么做?

Sel*_*enç 7

你可以使用HashSet:

var values = new HashSet<string>();
var xmlDocument = XDocument.Load("path");

foreach(var node in xmlDocument.Root.Elements("Node").ToList())
{
   if(!values.Add((string)node)) 
       node.Remove();
}

xmlDocument.Save("newpath");
Run Code Online (Sandbox Code Playgroud)

另一种方法是实现IEqualityComparerfor XElementclass然后使用Distinct方法.