开发者

How to count child XElements in where clause

开发者 https://www.devze.com 2023-02-10 21:16 出处:网络
<Automobiles> <Cars> <YearofMfr></YearofMfr> <Mileage></Mileage> <MeterReading></MeterReading>
<Automobiles>
  <Cars>
    <YearofMfr></YearofMfr>
    <Mileage></Mileage>
    <MeterReading></MeterReading>
    <Color></Color>
    <Condition></Condition>
  </Cars>
  <Cars>
    <YearofMfr></YearofMfr>
    <Color></Color>
    <Condition></Condition>
  </Cars>
</Automobiles>

How can I get an element which has all the child elements. To explain in detail. I have above xml. From this I want to retrieve single node which has all the child nodes. If you see in the second node some information is missing. I tried doing this.

var nodes = from nodeElements in doc.Descendants().FirstOrDefault().Elements()
                 where doc.Descendants().Count()==5
                select nodeElements;

I need a single node as output which has 5 child elements.

<Cars>     
<YearofMfr></YearofMfr>     
<Mileage></Mileage>     
<Meter开发者_StackOverflowReading></MeterReading>     
<Color></Color>     
<Condition></Condition>   
</Cars>


I suggest you select your count from nodeElements.Descendants instead:

var nodes = (from nodeElements in doc.Root.Elements()
            where nodeElements.Descendants().Count()==5
            select nodeElements).FirstOrDefault();

Updated to reflect the comment below and the comment to your original question.

0

精彩评论

暂无评论...
验证码 换一张
取 消