Question

I have an XML like this:

<root>
  <data>
   <_0>
    <value1></value1>
    <value2></value2>
   </_0>
   <_1>
    <value1></value1>
    <value2></value2>
   </_1>
 </data>
</root>

I want to get all descendants of the data node and currently using:

var descendants = from descendant in xdoc.Descendants("data")
                        select descendant;

But all that is returned is all of the text contained between the data element. How can I get a list of all descendants that start with different names like _0, _1, _2, etc. They will always be the direct descendants of the data node so there must be a way to get just those. Any help would be greatly appreciated since I have not worked much with LINQ. Thanks

Was it helpful?

Solution

You can Elements() to get the children. Like this, probably:

var descendants = from descendant in xdoc.Descendants("data")
                        select descendant.Elements();

Although it is probably cleaner to use the lambda synta instead of the query-comprehension syntax:

var descendants = xdoc.Descendants("data").Elements();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top