使用 XPath 识别 XML 中的重复节点 [英] Identify Duplicate Nodes in XML Using XPath

查看:49
本文介绍了使用 XPath 识别 XML 中的重复节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在以下 XML 结构中识别重复的组节点.我需要找到所有具有相同名称的组,无论它们在树中的哪个位置.

I am trying to identify duplicate group nodes in the following XML structure. I need to find all groups with the same name, wherever they are in the tree.

<report>
    <group name="a">
        <group name="1"></group>
        <group name="2"></group>
    </group>
    <group name="b">
        <group name="1"></group>
    </group>
</report>

类似于这篇文章(如何使用 XPathNavigator 来评估 XPath 1.0 中的重复节点?)但是,我需要识别具有相同属性而不是相同节点值的组.

Similar to this post (How do I identify duplicate nodes in XPath 1.0 using an XPathNavigator to evaluate?) However, I need to identify groups with the same attribute rather than the same node value.

推荐答案

如何使用 Linq To xml 查找重复项?

How about using Linq To xml to find duplicates?

var dubs = XDocument.Parse(xml)
            .Descendants("group")
            .GroupBy(g => (string)g.Attribute("name"))
            .Where(g => g.Count() > 1)
            .Select(g => g.Key);

这篇关于使用 XPath 识别 XML 中的重复节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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