Linq to XML & ASP.Net - part 5

This is the fifth post in a new series of posts regarding LINQ to XML and ASP.Net.Please do read the first post here. You can find the second post here .

You can find the third post here . You can read the fourth post here .

The xml file we are going to use in all these posts is the students.xml file. You can find the contents of this xml file in the first post.

I will use Visual Studio 2010 Ultimate edition and C# as the development language. Visual Studio 2010/2008 express editions will suffice.  

In this fourth post I will look at how to "How to find elements in a specific position within an xml file using LINQ to XML".

In this post we want to find e.g the second element, the last two elements, the first two elements.

1) Fire VS and create a web site. Choose an appropriate name for your site.

2) Add the existing students.xml file in your website.

3) In the code behind file Default.aspx.cs, add the namespace

using System.Xml.Linq;

All the classes we need to manipulate XML documents are inside this namespace.

In this first example we want to find the the information of the second element

In the Page_Load  event handling routine of the default.aspx.cs file, type the following

       XElement xelement = XElement.Load(@"C:\Users\fofo\Documents\
Visual Studio 2010\WebSites\forblog\xml\students.xml");


         var student = xelement.Descendants("Student").ElementAt(1);
         Response.Write(student);
 
4) We use the ElementAt method to return an element at a specified index at a sequence. The output of this LINQ to XML query will be
2 Mary Female ASP.Net Physics 

Ormond street Edinburgh FIFE 95701 U.K


So we get back all the information for the second student in the xml document.
5) Let's try another example. What if we wanted to list the first 2 elements and all their data?Comment out these lines of code
       
var student = xelement.Descendants("Student").ElementAt(1);
         Response.Write(student);
and type
var students = xelement.Descendants("Student").Take(2);
        foreach (var student in students)
            Response.Write(student);
 
Take operator gets the first x number of elements in a sequence.Run your application and see in the browser the following results.  
1 nikos Male Office 2010 Maths 

Kosti Palama Athens Attica 13231 GREECE
 

2 Mary Female ASP.Net Physics

Ormond street Edinburgh FIFE 95701 U.K
 
6) What if we wanted to retrieve the 2nd and 3rd element?Comment out these lines of code 
 
var students = xelement.Descendants("Student").Take(2);
        foreach (var student in students)
            Response.Write(student);
 
and type
 var students = xelement.Descendants("Student").Skip(1).Take(2);

        foreach (var student in students)
            Response.Write(student);
Skip operator gets all the elements after skipping x number of elements.
Run your application and see in the browser the following results.  
 
2 Mary Female ASP.Net Physics 
Ormond street Edinburgh FIFE 95701 U.K
 
3 Giacomo Male Silverlight Chemistry
Messsina road 12 Livorno Toscany 23701 Italy
 
7) What if we wanted to get the last two elements in the xml file?Comment out these lines of code 
 
var students = xelement.Descendants("Student").Skip(1).Take(2);

        foreach (var student in students)
            Response.Write(student);
and type
  var students = xelement.Descendants("Student").Reverse().Take(2);

        foreach (var student in students)
            Response.Write(student);
Reverse operator performs a reversal transformation on the elements.
Run your application and see in the browser the following results.   
4 Gaia Female English WPF 
Mopipetro 32 Milano Bergamo 43501 Italy
 
3 Giacomo Male Silverlight Chemistry
 
Messsina road 12 Livorno Toscany 23701 Italy

Email me if you need the source code. Hope its helps !!!!

No Comments