如何计算where子句中的子XElements

Saj*_*jid 0 xml linq xelement linq-to-xml

<Automobiles>
  <Cars>
    <YearofMfr></YearofMfr>
    <Mileage></Mileage>
    <MeterReading></MeterReading>
    <Color></Color>
    <Condition></Condition>
  </Cars>
  <Cars>
    <YearofMfr></YearofMfr>
    <Color></Color>
    <Condition></Condition>
  </Cars>
</Automobiles>
Run Code Online (Sandbox Code Playgroud)

如何获得包含所有子元素的元素.详细解释.我有上面的xml.从这里我想要检索具有所有子节点的单个节点.如果您在第二个节点中看到某些信息丢失.我试过这样做.

var nodes = from nodeElements in doc.Descendants().FirstOrDefault().Elements()
                 where doc.Descendants().Count()==5
                select nodeElements;
Run Code Online (Sandbox Code Playgroud)

我需要一个节点作为输出,它有5个子元素.

<Cars>     
<YearofMfr></YearofMfr>     
<Mileage></Mileage>     
<MeterReading></MeterReading>     
<Color></Color>     
<Condition></Condition>   
</Cars>
Run Code Online (Sandbox Code Playgroud)

Dan*_*rth 5

我建议您从nodeElements.Descendants中选择您的计数:

var nodes = (from nodeElements in doc.Root.Elements()
            where nodeElements.Descendants().Count()==5
            select nodeElements).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

已更新,以反映以下评论和对原始问题的评论.