Linq to XML introduction

In my last article I explained how we can create XML file using LINQ. In this article I will discuss how we can traverse this XML file using LINQ.

XML File

Suppose we have following XML file

		
	<?xml version=”1.0” encoding=”utf-8” ?>

<Techaltum>

<student Code=”101”>
        <name>isha malhotra</name>
        <id> 1 </id>
        <course> asp.net </course>
    </student>

<student Code=”102”>
        <name>Avi malhotra</name>
        <id> 2 </id>
        <course> Web Designing </course>
    </student>

<student Code=”103”>
        <name>Neha</name>
        <id> 3 </id>
        <course> Java </course>
    </student>

</Techaltum>

		
		

If I consider this xml file then in this file first line is declaration. Techaltum is an element and in student element code is attribute.

Load XML File

Before traversing the xml file first we have to load this file. We will use following code to load xml file

		
		XElement student = XElement.Load(Server.MapPath("xml") + "/abc.xml");
		
		

Select All Student Records

Suppose I have to show all the records of Student Element of XML File in GridView then we will write the following query:-

		
		//load xml file
        XElement student = XElement.Load(Server.MapPath("xml") + "/abc.xml");
        //query to show all data of student element
        var res = from name in student.Elements("student")
                  select new { StudentId = name.Element("id").Value, StudentName=name.Element("name").Value};

        GridView1.DataSource = res;
        GridView1.DataBind();

		
		

The output will be as follows:-

		
		LINQ to XML Traversal
		Figure 1

		
		

Show All Student Name start with N

Syntax of Attribute in LINQ:-

		
		//load xml file
        XElement student = XElement.Load(Server.MapPath("xml") + "/abc.xml");
        
        var res = from name in student.Elements("student")
                  where name.Element("name").Value.StartsWith("N")
                  select new { StudentId = name.Element("id").Value, StudentName=name.Element("name").Value};

        GridView1.DataSource = res;
        GridView1.DataBind();


		
		

The output will be as follows:-

		
		LINQ to XML Traversal with filter
		Figure 2

		
		

Attribute filtration

Following is the example where we filter data on the basis of attribute

		
		//load xml file
        XElement student = XElement.Load(Server.MapPath("xml") + "/abc.xml");
        
        var res = from name in student.Elements("student")
                  where name.Attribute("code").Value=="101"
                  select new { StudentId = name.Element("id").Value, StudentName=name.Element("name").Value};

        GridView1.DataSource = res;
        GridView1.DataBind();

	
		
		
		
		LINQ to XML Traversal with attribute filter
		Figure 3

		
		

Sorting of XML File

		
		//load xml file
        XElement student = XElement.Load(Server.MapPath("xml") + "/abc.xml");
        
        var res = from name in student.Elements("student")
                  orderby name.Element("name").Value descending
                  select new { StudentId = name.Element("id").Value, StudentName=name.Element("name").Value};

        GridView1.DataSource = res;
        GridView1.DataBind();

		
		

Note:- if you do not use descending keyword it will automatically sort in ascending order.

		
		LINQ to XML Traversal with sorting
		Figure 4
		
		

Email Address

For any query you can send mail at info@techaltum.com
Thanks