【发布时间】:2016-07-22 11:10:42
【问题描述】:
我有以下 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必须具有唯一值。如果我在schedule 中有1 2 3 2 ,则@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 会更好?
-
?我不明白你的回答.. linq 表达式就像 xpath。我正在用 linq 构建我的 xpath 表达式,但表达式不完整。
-
啊。您所拥有的不是 XPath,XPath 是一种用于寻址 XML 文档部分的特定语言。您正在使用 LINQ to XML,所以如果这不是您真正想要的,我建议您从问题中删除对 XPath 的引用。
-
//item[@orderno = preceding::item/@orderno] -
@splash58 你的 xpath 表达式和我的 linq 表达式完全一样
标签: c# xml linq linq-to-xml