中查找重複的子節點我有下面的XML文檔XML文檔
<xml>
<schedule orderno = "1">
<item orderno = "1" />
<item orderno = "2" />
<item orderno = "3" />
<item orderno = "2" />
</schedule>
<scool orderno = "2">
<item orderno = "5" />
<item orderno = "6" />
<item orderno = "1" />
<item orderno = "4" />
</scool>
</xml>
我在XML文件中的數據不一致,需要一個XPath表達式得到重複。
規則是每個節點scool/schedule
中來自item
的屬性@ordnerno
必須具有唯一值。如果我有1
2
3
2
in schedule
@orderno
與值2
重複且不一致。
我使用XML LINQ表達式庫
XDocument.Parse(structure)
.Descendants("item")
.Attributes("orderno")
.GroupBy(g => g.Value)
.Where(g => g.Count() > 1)
我的解決辦法是次優的,因爲這組的所有節點,schedule
和scool
。
輸出是1
和2
,但在這種情況下1
不是預期的。
我該如何解決我的問題?通過項目的母公司
爲什麼你認爲的XPath會更好:
更新在任何嵌套級別選擇具有重複
@orderno
節點? –?我不明白你的答案.. linq表達式就像xpath。我正在用linq構建我的xpath表達式,但表達式並不完整。 – mimu1011
啊。你得到的不是XPath,[XPath](https://en.wikipedia.org/wiki/XPath)是用於處理XML文檔部分的特定語言。您正在使用LINQ to XML,所以我建議您從問題中刪除對XPath的引用,如果這不是您真正想要的。 –