Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

LINQ to XML in ASP.NET

Author: Rudy Pinson
Download Source Code : 1329_LINQToXML.zip

LINQ to XML is one of the LINQ providers along with LINQ to Objects, LINQ to SQL and LINQ to DataSet. This article will explain that how we can use LINQ to XML in ASP.NET.

 

 

I have written a series of articles on DevASP.NET about LINQ. You can refer to these articles if you want to learn LINQ, LINQ to SQL and LINQ to DataSet.
 
LINQ to XML is a new and easy way to read and write XML data in ASP.NET. It is also called XLINQ. There are many situations where you need to deal with XML data source and LINQ to XML provides you an easy way to handle XML data in ASP.NET. First we need to convert XML file data to XAttribute, XDocument or XElement objects and then we can query these objects using LINQ to work with data in our application. 

 

  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Now add a XML file to Website
  4. Open Default.aspx page and drag and drop a GridView in the page.

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
     
  5. Open XMLfile.xml and write code below in it.

     
    <Employees>
           <Employee>
                  <FirstName>Rita</FirstName>
                  <LastName>Muller</LastName>
                  <Age>25</Age>
                  <HireDate>11/24/2011</HireDate>
           </Employee>
           <Employee>
                  <FirstName>Martin</FirstName>
                  <LastName>Sommer</LastName>
                  <Age>31</Age>
                  <HireDate>7/15/2007</HireDate>
           </Employee>
           <Employee>
                  <FirstName>Paul</FirstName>
                  <LastName>Lincoln</LastName>
                  <Age>29</Age>
                  <HireDate>5/8/2009</HireDate>
           </Employee>
    </Employees>
     
  6. Make sure that following namespaces exist in your code behind file
     
    C#

    using System.Linq;
    using System.Xml.Linq;
     
    VB.NET
     
    Imports System.Linq
    Imports System.Xml.Linq
     
     
  7. Now Write code below in Page Load event
     
    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        string file = Server.MapPath("XMLFile.xml");
        XElement element = XElement.Load(file);
     
        var employees = from employee in element.Descendants("Employee")
                        select new
                        {
                            FirstName = employee.Element("FirstName").Value,
                            LastName = employee.Element("LastName").Value,
                            Age = employee.Element("Age").Value,
                            HireDate = employee.Element("HireDate").Value,
                        };
     
        GridView1.DataSource = employees;
        GridView1.DataBind();
    }
     
    VB.NET
     
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim file As String = Server.MapPath("XMLFile.xml")
        Dim element As XElement = XElement.Load(file)
     
        Dim employees = From employee In element.Descendants("Employee")
                        Select New With
                        {
                            Key .FirstName = employee.Element("FirstName").Value,
                            Key .LastName = employee.Element("LastName").Value,
                            Key .Age = employee.Element("Age").Value,
                            Key .HireDate = employee.Element("HireDate").Value
                        }
     
        GridView1.DataSource = employees
        GridView1.DataBind()
    End Sub
     
    First we need to load XML file using Load() method of XElement class.  This Load() method is also available in XDocument class. Now query your XML file which we have converted to XElememt. Descendants() method of XElement class will return the collection in the descendant element.  Now get each element value using Element() method. At the end, bind ‘employees’ variable to GridView control.
     
  8. View the Web Site in browser and see result.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to convert DataSet to ArrayList and ArrayList to DataSet in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net