Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first level of children by LINQ

Tags:

xml

asp.net

linq

I have such XMl

<root>
    <list>
        <list>
            <topic></topic>
            <topic></topic>
        </list>
        <topic></topic>
        <topic></topic>
    </list>
    <topic></topic>
    <topic></topic>
    <topic></topic>
</root>

I need to get the first level of children:

<list></list>
<topic></topic>
<topic></topic>
<topic></topic>

I try to do like this

var list = x.Descendants().Where(e => e.Name == "list" || e.Name == "topic");

But it returns all topics and lists.

Please help! :)

like image 622
podeig Avatar asked Jun 22 '10 08:06

podeig


1 Answers

Just document.Root.Elements() should work.

Basically Descendants() recurses, whereas Elements() only gets direct children.

like image 161
Jon Skeet Avatar answered Oct 06 '22 15:10

Jon Skeet