Personal Blog of Mustafa Basgun

“LINQ to XML” in ASP.NET – Ajaxified XML Document Filtering

with 6 comments


LINQ to XML is a new way to play with the XML data in .NET 3.5. It is actually an XML programming API that simplifies dealing with XML documents without any need to know about additional syntaxes such as XSLT and XPath. It is designed to be a cleaner, faster and lightweight API which at the same time has the key features from XSLT and XPath. According to MSDN, its public data model is aligned as much as possible with the W3C XML Information Set.

Sample XML Data and Its Filtering with “LINQ to XML”

For sampling purposes, I just created the following XML data and add it to my demo project:

My purpose is developing a simple ajaxified web application that has a dropdown with the country names and a literal that shows the capital city of selected country name from this dropdown. For this purpose, I created my server controls with some AJAX flavor as:

Then, I organized the Page_Load event as in the following in order to query over Countries.xml:

Pay attention to the usage of XDocument class from System.Xml.Linq namespace (which is already defined in the web.config file as default) in order to load the local Countries.xml file and the new casting and access methods (such as .Descendants and .Element that I used above).

Finally, after compilation the result on the browser should be like:

When “Belgium” is selected from the dropdown, literal will show the following with a partial rendering:


I generally work with SQL Server or Access in the data layers of my web applications, therefore I barely had a chance to put my hands on XSLT and XPath deeply. But, I can say that “LINQ to XML” as a data provider seems enough even powerful for me to handle many common XML programming tasks inside my code.


You can download the demo project via here.


Written by Mustafa Basgun

January 13, 2008 at 10:00 AM

6 Responses

Subscribe to comments with RSS.

  1. Very clear and great posts Mustafa. How about putting the live demos of your sample apps?

    Mark W.

    January 14, 2008 at 6:07 PM

  2. Mark,

    I have been having an intent to do something like that. I am planning to deal with it at my first chance sometime within the next couple of months.

    Mustafa Basgun

    January 15, 2008 at 11:16 AM

  3. Which reference and imports are needed to use LINQ to XML? Thanks.


    December 25, 2008 at 1:35 PM

  4. Andy,

    Following MSDN link should give you an idea:

    Mustafa Basgun

    December 25, 2008 at 9:40 PM

  5. How can I load and manipulate an XML file which has schema? Does LINQ to XML provide a way for this?


    March 25, 2009 at 3:15 AM

  6. Yes, it’s useful for the ASP.NET developers.

    Vaibhav Kothia

    April 5, 2010 at 6:10 AM

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: