Writing a LINQ query on an XML data file: Step by step explanation in VB.NET (VS2008)

Introduction

I had to create a working prototype of a web project. I mocked up screens, created some fake data in an XML file, showed it on screens. Now, I wanted to work on the data (query XML file and present on other screens). I know that it’s achievable through LINQ. I searched for snippets online. I was able to find some help, but had to spend a lot of time. This led me to post this topic. Hope it would be helpful for a few beginners.

Using the code

Included is a Zip file containing the two ASP pages and an XML file. Feel free to use them. I am not going to explain anything here. Instead, I shall show you the step by step process to create the project, and you can go through the code.

Open VS 2008. Create a new web project by selecting the ASP.NET Web Site template. I name the project, “LinqDemo”. Select the language as “VisualBasic” and .NET framework 3.5.

Delete the default.aspx page. Add a new form, Employee.aspx, and set it as the start page. Modify the code for the page, Employee.aspx, to show as follows:

Employee.aspx:

Add the Employee.xml file to the App_Data folder in your project. Modify the file to show data as follows: