Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用degeneratns实现LINQ到XML查询_C#_Linq To Xml - Fatal编程技术网

C# 使用degeneratns实现LINQ到XML查询

C# 使用degeneratns实现LINQ到XML查询,c#,linq-to-xml,C#,Linq To Xml,我想使用LINQ to xml查询以下xml文件 <table> <row> <cell> <content>x</content> <cell> <cell> <content>y</content> <cell> <cell> <foo> <bar>x</bar>

我想使用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
Y
x

我正在尝试获取所有具有值为“x”的子体的单元节点。在本例中,应返回两个单元格节点

您可以使用
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;

元素“bar”下的“x”呢?我希望有两个结果(包括在bar下包含x的单元格)@markus:我已经修改了我的答案,现在应该给出正确的结果了。