Select Data From XML With Where Clause Using LINQ to XML

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in LINQ to XML.

Question: What is select data from XML with where clause using LINQ to XML?

In simple terms "It provides flexibility to pull out the data from XML using a LINQ query by filtering the data based on element."

Step 1: Create a new "ASP.Net Web Forms Application"

Output1.jpg

Step 2: The complete code of Employee.xml looks like this
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Employees>  
  3.   <Employee>  
  4.     <FirstName>Vijay</FirstName>  
  5.     <Age>26</Age>  
  6.   </Employee>  
  7.   <Employee>  
  8.     <FirstName>Ram Reddy</FirstName>  
  9.     <Age>29</Age>  
  10.   </Employee>  
  11. </Employees>
Step 3: The complete code of webform1.aspx looks like this
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LINQtoXMLSelectApp.WebForm1" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <center>  
  10.         <div>  
  11.             <table>  
  12.                 <tr>  
  13.                     <td colspan="2">  
  14.                         <asp:Label ID="Label1" runat="server" Text="Select Data with Where Clause using LINQ to XML"  
  15.                             Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>  
  16.                     </td>  
  17.                 </tr>  
  18.                 <tr>  
  19.                     <td colspan="2" align="center">  
  20.                         <asp:Button ID="Button1" runat="server" Text="Select Data" Font-Names="Verdana" Width="213px"  
  21.                             BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />  
  22.                     </td>  
  23.                 </tr>  
  24.                 <tr>  
  25.                     <td colspan="2" align="center">  
  26.                         <br />  
  27.                         <asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"  
  28.                             BorderWidth="1px" CellPadding="2" EnableModelValidation="True" ForeColor="Black"  
  29.                             GridLines="None" AutoGenerateColumns="False">  
  30.                             <AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle>  
  31.                             <FooterStyle BackColor="Tan"></FooterStyle>  
  32.                             <HeaderStyle BackColor="Tan" Font-Bold="True"></HeaderStyle>  
  33.                             <PagerStyle HorizontalAlign="Center" BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue">  
  34.                             </PagerStyle>  
  35.                             <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite"></SelectedRowStyle>  
  36.                             <Columns>  
  37.                                 <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="true" />  
  38.                                 <asp:BoundField DataField="Age" HeaderText="Age" ReadOnly="true" />  
  39.                             </Columns>  
  40.                         </asp:GridView>  
  41.                     </td>  
  42.                 </tr>  
  43.             </table>  
  44.         </div>  
  45.     </center>  
  46.     </form>  
  47. </body>  
  48. </html>
Step 4: The complete code of webform1.aspx.cs looks like this
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Xml.Linq;  
  8. namespace LINQtoXMLSelectApp  
  9. {  
  10.     public partial class WebForm1 : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.         }  
  15.         protected void Button1_Click(object sender, EventArgs e)  
  16.         {  
  17.             XDocument document = XDocument.Load(@"c:\users\administrator\documents\visual studio 2010\Projects\LINQtoXMLSelectApp\LINQtoXMLSelectApp\Employee.xml");  
  18.             var query = from r in document.Descendants("Employee") where (int)r.Element("Age") > 27 select new  
  19.             {  
  20.                 FirstName = r.Element("FirstName").Value, Age = r.Element("Age").Value };  
  21.             GridView1.DataSource = query;  
  22.             GridView1.DataBind();  
  23.         }  
  24.     }  
  25. }
Step 5: The output of the application looks like this 

Output2.png 

Step 6: The filtered data selected from XML looks like this

 Output3.png

I hope this article is useful for you.


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.