如何使用 Linq to XML 查询搜索节点?
在 xml 文档下面
<Root>
<Global>
</Global>
<local>
<section name="A">
<subsection name="A">
<innersection name="A">
<Property1>
</Property1>
</innersection>
<innersection name="B">
<Property1>
</Property1>
</innersection>
</subsection>
<subsection name="B">
<innersection name="A">
<Property1>
</Property1>
</innersection>
<innersection name="B">
<Property1>
</Property1>
</innersection>
</subsection>
</section>
<section name="B">
<subsection name="A">
<innersection name="A">
<Property1>
</Property1>
</innersection>
<innersection name="B">
<Property1>
</Property1>
</innersection>
</subsection>
<subsection name="B">
<innersection name="A">
<Property1>
</Property1>
</innersection>
<innersection name="B">
<Property1>
</Property1>
</innersection>
</subsection>
</section>
</local>
</Root>
现在我想要使用 linq to xml 在一个查询中使用其节名称 =“B”、小节名称 =“B”和内部节名称 =“B”的 property1。
Below the xml doc
<Root>
<Global>
</Global>
<local>
<section name="A">
<subsection name="A">
<innersection name="A">
<Property1>
</Property1>
</innersection>
<innersection name="B">
<Property1>
</Property1>
</innersection>
</subsection>
<subsection name="B">
<innersection name="A">
<Property1>
</Property1>
</innersection>
<innersection name="B">
<Property1>
</Property1>
</innersection>
</subsection>
</section>
<section name="B">
<subsection name="A">
<innersection name="A">
<Property1>
</Property1>
</innersection>
<innersection name="B">
<Property1>
</Property1>
</innersection>
</subsection>
<subsection name="B">
<innersection name="A">
<Property1>
</Property1>
</innersection>
<innersection name="B">
<Property1>
</Property1>
</innersection>
</subsection>
</section>
</local>
</Root>
Now i want the property1 whose section name="B" and subsection name="B" and innersection name="B" in one single query using linq to xml.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的看法,替代 Jon 的看法,假设 Property1 在内部部分中仅出现一次,并且您只需要那个:
Here is my take, alternative to Jon's, assuming Property1 occurs only once inside an innersection and you need only that one:
编辑:删除了 LINQ to XML“正常”样式的查询,因为 ssg 的查询更好。不过我想保留 XPath 版本。它未经测试,但我认为它应该有效......
EDIT: Removed LINQ to XML "normal" style of query as ssg's is better. I wanted to leave the XPath version though. It's untested, but I think it should work...
由于这个问题也被标记为 vb.net,因此这里相当于 ssg 在 vb.net 中的查询:
Since this question was also tagged vb.net, here is the equivalent of ssg's query in vb.net: