如何使用C#在XML中查找重复节点 [英] How to find the duplicate nodes in XML using C#
本文介绍了如何使用C#在XML中查找重复节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有非结构化XML,如下所示
I have unstructured XML as shown below
<Root name="FIRST" serviceidentifier="">
<SERVER response="SERVICE NOT FOUND" type="SERVER" identifier="NONE" misc="NOT FOUND"/>
</Root>
<Root name="SECOND" serviceidentifier="">
<SERVER response="SERVICE NOT FOUND" type="SERVER" identifier="NONE" misc="NOT FOUND"/>
</Root>
<Root name="FIRST" serviceidentifier="">
<SERVER response="SERVICE NOT FOUND" type="SERVER" identifier="NONE" misc="NOT FOUND"/>
</Root>
我必须找到具有重复节点的XML的行号,其属性为首先喜欢根名称= FIRST
我尝试过:
我是XML新手所以请给我一个完整的代码
感谢您的帮助.....
I Have to find the line number of the XML which has duplicate nodes with the attribute "FIRST" like "Root name=FIRST"
What I have tried:
I am New to XML So please give me a Complet CODE
Thanks for your help .....
推荐答案
您的第一个也是最严重的问题是这不是XML(无效),因此您无法将其加载到 XmlDocument [ ^ ] object ...
首先将其包装到虚拟根目录中使其成为有效的XML,而不是使用XMLDocument和XPath来查找具有相同属性的元素......
XPath示例 [ ^ ]
[ ^ ]
< a href =https://docs.microsoft.com/en-us/dotnet/standard/data/xml/select-nodes-using-xpath-navigation>使用XPath导航选择节点Microsoft Docs [ ^ ]
Your first and most severe problem is that this is not an XML (not valid), so you can not load it into an XmlDocument[^] object...
So first wrap it into a virtual root make it valid XML, than use XMLDocument and XPath to find elements with the same attributes...
XPath Examples[^]
[^]
Select Nodes Using XPath Navigation | Microsoft Docs[^]
这篇关于如何使用C#在XML中查找重复节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文