“LINQ to XML” in ASP.NET - Ajaxified XML Document Filtering
Introduction
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:

Conclusion
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.
Download
You can download the demo project via here.
Simple Experiment with LINQ
LINQ, Language Integrated Query, is a new feature in the version of 3.5. I haven’t had a chance to play with it before, so I thought making some 101 type simple experiments would be a great starting point.
When you create a new ASP.NET web project with VB.NET in VWD 2008, web.config file already comes with the required assemblies and namespaces.

Introduction
Let’s have a simple array with some different country names in it, and try to find the country names starting with letter A within this array by using non-LINQ (which I call “traditional”) and LINQ ways.

Solutions
In order to see the basic usage of LINQ, I will use the literal control.

First literal will show the results when CNames is looped through all country names and all these country names are checked whether they start with A or not, whereas second literal will show the result of its associated LINQ query.

Pay attention to the Order By part of the query which sorts the result easily.
Now, I will use a DropDownList control in order to see what happens if I would use the result of a LINQ query as a direct data source.

Like in the “literal” approach, first dropdown will show the results when CNames is looped through all country names and selected country names will added to an arraylist (which will be the data source of this first dropdown) in order to have a sorting capability. For the second dropdown, I will set the result of LINQ query as a direct data source to it.

The concept of “data source” is now having an evolution with LINQ. Depending on the application scenarios, I guess anything can be a direct data source for the data controls.
After writing all these codes, browser looks like:

Conclusion
I haven’t touched to LINQDataSource yet, but this post gives me the enough initial opinion that LINQ seems to be a great addition to the language. It makes more sense to me when I read the LINQ’ed code as a developer point of view, and I believe it would be easier to enhance or modify the application when its code is more readable. The only issue is I have been used to the syntagm of “Select From Where” instead of using From at the beginning and Select later. Anyway, I will get used to it.
Download
To play around with this sample, you can download the code via here. For deeper LINQ posts, visit ScottGu’s Blog.