ASP.NET Code to Read and Display XML file in GridView Control

RecordDisplaybyBiningXML.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RecordDisplaybyBiningXML.aspx.cs" Inherits="WebDemo01.RecordDisplaybyBiningXML" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>
    ASP.NET Code to Read and Display XML file in GridView Control
    </title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        <h3>
                        Candidate Records (Data Source : XML File)</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:GridView ID="GridView1" runat="server"                                 Width="600px" AutoGenerateColumns="false">

                            <Columns>
                                <asp:BoundField DataField="Id"                                             HeaderText="ID" />
                                <asp:BoundField DataField="Batch"                                           HeaderText="Batch" />
                                <asp:BoundField DataField="TraineeName"                                     HeaderText="Name" />
                                <asp:BoundField DataField="State"                                           HeaderText="Coming from" />
                            </Columns>

                        </asp:GridView>

                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Add an XML file "xml_Trainees.xml" into your project by Solution Explorer. write the following content into that XML file you have added.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Trainees>
 
  <Trainee Id ="1" Batch ="CHN19DN017">
    <TraineeName>Ram Pranav</TraineeName>
    <State>Andra</State>
  </Trainee>
 
  <Trainee Id ="2" Batch ="CHN19DN017">
    <TraineeName>Partha Krishnan</TraineeName>
    <State>Andra</State>
  </Trainee>
 
  <Trainee Id ="3" Batch ="CHN19DN017">
    <TraineeName>Abinesh</TraineeName>
    <State>Tamilnadu</State>
  </Trainee>
 
  <Trainee Id ="4" Batch ="CHN19DN017">
    <TraineeName>Akash</TraineeName>
    <State>Tamilnadu</State>
  </Trainee>
 
  <Trainee Id ="5" Batch ="MNSTCK02">
    <TraineeName>Saksham</TraineeName>
    <State>Uttarakhand</State>
  </Trainee>
 
  <Trainee Id ="6" Batch ="MNSTCK02">
    <TraineeName>Aravind</TraineeName>
    <State>Tamilnadu</State>
  </Trainee>
 
  <Trainee Id ="7" Batch ="MNSTCK02">
    <TraineeName>Sai Kiran</TraineeName>
    <State>Telungana</State>
  </Trainee>
 
  <Trainee Id ="8" Batch ="MNSTCK02">
    <TraineeName>Nandhini</TraineeName>
    <State>Tamilnadu</State>
  </Trainee>
 
  <Trainee Id ="9" Batch ="MNSTCK02">
    <TraineeName>Anjana</TraineeName>
    <State>Telungana</State>
  </Trainee>
 
</Trainees>

RecordDisplaybyBiningXML.aspx.cs

using System;
//added...
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace WebDemo01
{
    public partial class RecordDisplaybyBiningXML : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
              DisplayCandidateRecords();
        }

        private void DisplayCandidateRecords()
        {
            //Prerequistics
            string filePath = Server.MapPath("xml_Trainees.xml");
            DataSet traineesDataSet = new DataSet();

            //Read the contents of the XML file into the DataSet
            traineesDataSet.ReadXml(filePath);

            //Binding Datasource
            GridView1.DataSource = traineesDataSet.Tables[0].DefaultView;
            GridView1.DataBind();

            traineesDataSet.Clear();
        }
    }
}

No comments:

Post a Comment