Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

Linq to XML & ASP.Net - part 2

This is the second post in a new series of posts regarding LINQ to XML and ASP.Net.Please do read the first article 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 link above.

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

In this second post I will look at how to "Read an element inside another element in an XML document using LINQ to XML".


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 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 addresses = from address in xelement.Elements("Student")
       where (string)address.Element("Address").Element("City") == "Athens" &&
       (string)address.Element("Sex").Value == "Male"
       select address;
            
Response.Write("Address details of students living in Athens: ");
            Response.Write("<br/>");
            
foreach (XElement xEle in addresses)
            {   
        Response.Write(xEle.Element("Address").Element("Street").Value);
        Response.Write("<br/>");
        Response.Write(xEle.Element("Address").Element("State").Value);
        Response.Write("<br/>");
        Response.Write(xEle.Element("Address").Element("Zip").Value);
        Response.Write("<br/>");
        Response.Write(xEle.Element("Address").Element("Country").Value);
        Response.Write("<br/>");
            }
 
4)It is very easy to see what i am doing here. If you look more carefully at our students.xml we see that the <Address> element has nested elements within. We need to access the data inside the nested elements.
         <Address>
            <Street>Kosti Palama</Street>
            <City>Athens</City>
            <State>Attica</State>
            <Zip>13231</Zip>
            <Country>GREECE</Country>
        </Address>

We see that these elements <Street>, <City>, <State>, <Zip>, <Country> are inside the <Address> element.
We just find the element within the element with this line of code
address.Element("Address").Element("City")
I think we can see in the Linq query the power of LINQ. We do query the xml file the same way we would query another data source. So no need to learn different APIs for different data sources.
5) Run your application and see in the browser the following results.
Address details of students living in Athens: 
Kosti Palama
Attica
13231
GREECE

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

Comments

No Comments