This tutorial will show you how to implement AJAX, LINQ and XML altogether to create a web application that can both read and save XML data. C# version.

This tutorial was written with Visual Studio.NET 2008, so AJAX is fully integrated. You can use 2005, but you will need to download and install the AJAX Extensions from the Microsoft website.

AJAX and LINQ are probably the most-talked about Microsoft offerings at the moment, and rightly-so. Both provide us with powerful features to add to our development arsenal- AJAX allows us to create more user-friendly web applications that can almost rival our desktop equivalents; and then LINQ provides a unified way for us to communicate and interact with a wide array of data sources.
This tutorial will combine the two, and throw in a bit of XML too. In this example we are going to create a web application that will use an XML file to store small amounts of data, that we will display and also add to using AJAX and LINQ.

To demonstrate this, we will be storing names, cities and ages of people in the XML file. We will use a literal control to display the XML data and three textboxes to input new data.
Our XML file structure will look something like this:

Because we are using VS.NET 2008, all we need to do to enable AJAX on our page is add the following:

This is the AJAX structure we will be using for our page. The ScriptManager will handle all of our AJAX calls, we don’t need to do anything with that. The UpdatePanel specifies an area of the page that will partially refresh- instead of posting back the whole page. We will also add a trigger, to specify what will ‘trigger’ the partial refresh. To do this, though, we need the name of our button.

We add a button and the trigger to the page:

We reference the button in the trigger field of the UpdatePanel, which will cause the partial refresh to initiate when the button is clicked.

Next, we can add the rest of our form – the textboxes for data input, and the literal control for reading the XML. We can also add another button to initiate the reading of the XML file:

Our ASPX page is now complete. We have means to both read XML data and also add new data.
Notice that both of our buttons have the onclick handlers specified. We can now code these in the code-behind, like so:

We tell the read button to call another method when it is clicked because we can reuse it – notice we call the same method at the end of the add button. This is so we can update the displayed XML when we add new content. We are using LINQ to open the XML document first, and then add a new element into the parent element of Persons. Then we save changes to the XML file and let the user know.

The readXML method looks like this:

As you can see, LINQ looks similar to SQL, but instead of communicating with a database only, we can use LINQ to communication with any data source – in this case an XML file. First we declare a variable persons and assign to it the collection from the XML file’s descendants, Person. Then we select all the data from within and simply loop through our collection and output to the literal control.