Creating a LINQ to XML Application
We can display XML data as our output by using LINQ to XML Application.



For this we add a Default.aspx page in our project and write the following code in our application:-

  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <center> 
    <div>
        <h1>Linq To XML Application:-</h1>
        <asp:ListBox ID="lbDetail" runat="server" Height="300px" Width="400px"></asp:ListBox> <br /> <br />
        <asp:Button ID="btnDisplayRecord" runat="server" Text="Display Record" Font-Bold="true" OnClick="btnDisplayRecord_Click" />
    
    </div>
            </center>
    </form>
</body>
</html>




Double click on DisplayRecord button and write the below code:-

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  
    protected void btnDisplayRecord_Click(object sender, EventArgs e)
    {
        lbDetail.Items.Clear();

        try
        {
            XElement Students = XElement.Parse
                (
                @"<Students>
                     <stu>
                     <Rollno>3001</Rollno>
                     <Name>Abhinav Kumar</Name>
                     <Branch>IT</Branch>
                     </stu>

                    <stu>
                     <Rollno>3030</Rollno>
                     <Name>Pramod Kumar Sah</Name>
                     <Branch>CSE</Branch>
                     </stu>

                     <stu>
                     <Rollno>3033</Rollno>
                     <Name>Santosh Kumar Singh</Name>
                     <Branch>IT</Branch>
                     </stu>

                    <stu>
                     <Rollno>3031</Rollno>
                     <Name>Pooja Kumari</Name>
                     <Branch>ECE</Branch>
                     </stu>

                    <stu>
                     <Rollno>3003</Rollno>
                     <Name>Abhishek Arun</Name>
                     <Branch>IT</Branch>
                     </stu>

                    <stu>
                     <Rollno>3030</Rollno>
                     <Name>Pramod Kumar Sah</Name>
                     <Branch>CSE</Branch>
                     </stu>

                   <stu>
                     <Rollno>2008</Rollno>
                     <Name>Rani Gupta</Name>
                     <Branch>IT</Branch>
                     </stu>

                    <stu>
                     <Rollno>420</Rollno>
                     <Name>Neha Sharma</Name>
                     <Branch>IT</Branch>
                     </stu>

                    <stu>
                     <Rollno>421</Rollno>
                     <Name>Ajay Kumar</Name>
                     <Branch>MCA</Branch>
                     </stu>

                    <stu>
                     <Rollno>423</Rollno>
                     <Name>Pramod Kumar Sah</Name>
                     <Branch>IT</Branch>
                     </stu>
                  </Students>
                 ");

            lbDetail.Items.Add("Display the Student Record who branch is Information Technology:- ");
            //var query = from stu1 in stu1.Elements("stu") where (string)stu.Element("Branch") = "IT" select stu.Element("Name");
            var query = from student in Students.Elements("stu") where (string)student.Element("Branch") == "IT" select student.Element("Name");
            foreach (var name in query)
            {
                lbDetail.Items.Add(name.Value);
            }
        }
        catch (Exception ex)
        {
            lbDetail.Items.Add(ex.Message);
        }
   
    }


}



Now run the application and get output as following:-