如何查找完全位于两个指定节点之间的节点
在如下所示的 XML 文档中:
<root>
<fish value="Start"/>
<pointlessContainer>
<anotherNode value="Pick me!"/>
<anotherNode value="Pick me too!"/>
<fish value="End"/>
</pointlessContainer>
</root>
如何使用 LINQ to XML 的神奇功能来查找 fish
节点完全包含的任何节点?请注意,在此示例中,我特意将 fish
节点放置在文档中的不同级别,因为我预计这种情况会在野外发生。
显然,在这个示例中,我希望获取两个 anotherNode
节点,而不是 pointlessContainer
节点。
注意:两个“定界”节点可能与文档中的其他非定界节点具有相同的类型(例如fish
),但它们将具有唯一的属性,因此易于识别。
In a XML document such as the following:
<root>
<fish value="Start"/>
<pointlessContainer>
<anotherNode value="Pick me!"/>
<anotherNode value="Pick me too!"/>
<fish value="End"/>
</pointlessContainer>
</root>
How can I use the wonder of LINQ to XML to find any nodes completely contained by the fish
nodes? Note that in this example, I have deliberately placed the fish
nodes at different levels in the document, as I anticipate this scenario will occur in the wild.
Obviously, in this example, I would be looking to get the two anotherNode
nodes, but not the pointlessContainer
node.
NB: the two 'delimiting' nodes may have the same type (e.g. fish
) as other non-delimiting nodes in the document, but they would have unique attributes and therefore be easy to identify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的示例,应该执行以下操作,
但我还没有测试或彻底思考它是否适用于其他情况。也许您可以检查一些示例数据并报告其是否有效。
For your sample, the following should do
But I haven't tested or thoroughly pondered whether it works for other cases. Maybe you can check for some of your sample data and report back whether it works.