什么是找到并删除里面的XML重复节点最快的方法是什么? [英] What's the fastest way to find and delete duplicate nodes inside XML?

查看:605
本文介绍了什么是找到并删除里面的XML重复节点最快的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML文件中有这样的结构

XML file has structure like this

<Nodes>
   <Node> one </Node>
   <Node> two </Node>
   <Node> three </Node>
   <Node> three </Node>
</Nodes>

由于XML文件有超过30000的节点我在找最快的方式找到并删除重复的节点。

Since xml file has more than 30000 nodes I'm looking for fastest way to find and delete duplicate nodes.

你会怎么做呢?

推荐答案

您可以使用的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");

另一种方法是实施的IEqualityComparer 的XElement 类,然后使用分明方法。

Another way is to implement an IEqualityComparer for XElement class then use Distinct method.

这篇关于什么是找到并删除里面的XML重复节点最快的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆