简体   繁体   中英

LINQ To XML Query using descendatns

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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