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>
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With