简体   繁体   English

LINQ 到 XML 使用后代查询

[英]LINQ To XML Query using descendatns

I want to query the following xml file using LINQ To XML我想查询下面的 xml 文件使用 LINQ 到 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'.我试图获取所有具有值为“x”的后代的单元节点。 In this example two cell nodes should be returned在此示例中,应返回两个单元节点

You can use the Any extension method to see if any of the descendents of cell have the correct value.您可以使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM