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:-
Double click on DisplayRecord button and write the below code:-
Now run the application and get output as following:-
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" %>
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 ( @" "); 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); } } } 3001 Abhinav Kumar IT 3030 Pramod Kumar Sah CSE 3033 Santosh Kumar Singh IT 3031 Pooja Kumari ECE 3003 Abhishek Arun IT 3030 Pramod Kumar Sah CSE 2008 Rani Gupta IT 420 Neha Sharma IT 421 Ajay Kumar MCA 423 Pramod Kumar Sah IT
Now run the application and get output as following:-