Linq and XML
Written by Mike James   
Tuesday, 28 October 2014
Article Index
Linq and XML
Select And Project

Select and the Projection

The Select part of a Linq expression normally works as a projection operator that “reduces” the size of the data structure returned by a query.

For example, it can be use to select which “columns” are returned from a SQL query which otherwise would return a complete record.

For example, the query

var q = from E in root.Elements()
  where E.Name == "Name"
   select E;

returns a deep copy in the sense that the XElement corresponding to Name brings with it an entire subtree.

Suppose you want a “shallow” copy, i.e. just the node and its text value. In this case we can project the XElement to a string that contains all of the XText in the subtree:

var q = from E in root.Elements()
 where E.Name == "Name"
  select E.Value;

However, projection can also be used to create a larger or completely different type using the data returned by the query.

For example, you could perform a Linq to SQL query to return some values and then package them up into an XML tree.

To understand how this works all you really have to do is focus on the role of the “range” variable. It is set to each of the objects that match the selection criterion and eventually this can be used to construct any other data type.

For example, suppose you wanted to reconfigure the XML tree that stored the first and second names in our example into a different XML tree. You could do the job using a slightly more advanced select projection:

var q = from E in root.Elements()
 where E.Name == "Name"
  select new XElement("NameRecord",
    new XElement("Name1",
    new XElement("Name2",

In this case we take each of the selected elements and build a new XML tree using their values and it is a collection of these new XML trees that are returned.

While this isn’t a particularly useful transformation it gives you the basic idea, i.e. use the result of the query to build new types, and you can generalise it to other situations. In more complex transformations it can even be useful to work out intermediate results to be used later within the select clause.

To create a variable within a Linq expression you simply use the let keyword. For example the previous query can be re-written:

var q = from E in root.Elements()
 where E.Name == "Name"
  let z1=E.Element("First")
  let z2=E.Element("Second")
  new XElement("NameRecord",
  new XElement("Name1",z1.Value),
  new XElement("Name2",z2.Value));

In this case the let keywords create two new variables z1 and z2 which are used to hold the child elements that match the two sub-queries. Notice that the result is exactly the same if you don’t use the temporary variables.

Notice that you can even use the select to modify the existing XML tree using SetValue and so on but – be warned a tree is a complicated structure and you need to be sure that what you are trying to do can be done in every case.

In most cases it is much better to use the functional approach to build a new tree as part of the select clause. Notice that this is made even more powerful by the simple fact that the constructors can accept IEnumeration sequences and will automatically iterate through all of the objects thus adding them to the new tree.

For example, consider:

var q = from E in root.Elements()
 where E.Name == "Name"
   new XElement("NameRecord",
   new XElement("Name1",
   new XElement("Name2",

Notice that the use of root.Attributes adds all of the attributes in the collection to the new XElement corresponding to the tag <NameRecord>.

Linq, and Linq to XML in particular, provides so many ways of doing things that it can leave you feeling slightly queasy about the whole thing and it is certain that you can write code that is deep and impenetrable – don’t.


Related Articles

The LINQ principle

XML in C# - Using XElement

Programmer's Introduction to XML


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.



Deep C# - Inheritance

Inheritance is a simple idea, until you try to make use of it. What's a virtual method? How do you use override and new? Its tricky when you look at the details.

.NET Regular Expressions In Depth

If you think regular expressions are trivial and boring, you've not seen the whole picture. Here we reveal that in .NET they are amazingly powerful and not to be missed.

Other Articles






or email your comment to:







Last Updated ( Tuesday, 20 January 2015 )