This is a small post regarding the confusion that most XLINQ developers have when they start learning it, that what is the difference between Elements and Descendants?

Well basically both of them take the same arguments and also both return IEnumerable of type XElement, so what makes them different?
Lets take an Example :
here is the XML(it has a root node “users”) :

XML

So the exercise is the return the collection of node “mname” :
First let us use Elements,

As you can see to get to that node we have to start from the top and reach mname node.
Not to forget that, the above code will return only the first user(ie. Tarun), it won’t go to the next user. I would suggest you not to use Elements.

But that’s not the case with Descendants. With Descendants, it returns all the node mname, irrespective of its parent node.
Like this :

About

Hi! This is Tarun Kumar Singh. I am a software engineer with 1.8 year of experience.

This blog discusses the two awesome technologies- WPF and LINQ(Both self-taught) introduced in .NET 3.0 framework. I am truly amazed with the power of WPF together with the API’s of LINQ which makes work a lot easier and fast too!