使用 Descendatns 的 LINQ To XML 查询

发布于 2024-11-14 20:12:06 字数 392 浏览 2 评论 0原文

我想使用 LINQ To XML 查询以下 xml 文件,

<table>
 <row>
  <cell>
    <content>x</content>
  <cell>
  <cell>
    <content>y</content>
  <cell>
  <cell>
    <foo>
     <bar>x</bar>
    </foo>
  <cell>
 <row>
</table>

我试图获取所有具有值为“x”的后代的单元格节点。在此示例中,应返回两个单元节点

I want to query the following xml file using LINQ To XML

<table>
 <row>
  <cell>
    <content>x</content>
  <cell>
  <cell>
    <content>y</content>
  <cell>
  <cell>
    <foo>
     <bar>x</bar>
    </foo>
  <cell>
 <row>
</table>

Im trying to get all cell nodes that have a descendant with the value 'x'. In this example two cell nodes should be returned

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-11-21 20:12:06

您可以使用 Any 扩展方法来查看单元格的任何后代是否具有正确的值。

XDocument doc = XDocument.Load("somefile.xml");
var cells = from cell in doc.Descendants("cell")
            where cell.Descendants().Any(v => v.Value == "x")
            select cell;

You can use the Any extension method to see if any of the descendents of cell have the correct value.

XDocument doc = XDocument.Load("somefile.xml");
var cells = from cell in doc.Descendants("cell")
            where cell.Descendants().Any(v => v.Value == "x")
            select cell;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文