What is XML ?
XML is a markup language which is used to describe the data and transfer the data from one application to another application .XML is a language which is platform independent format so it can used on Internet "Heterogeneous environment" to transfer the data and to describe the data.
So let's create a application using Visual studio and create ASP .NET Web application:
Step-1: Open VS 2017 >> File >> New Project >> ASP .NET web application
Select Web-Forms |
This is the application folder |
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XMLBindGrid._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView runat="server" ID="Grid_employee" CssClass="table table-bordered">
</asp:GridView>
</asp:Content>
(i) Right Click Solution Folder >> Add New Item Select XML file
(ii) Add the dummy data into the "Employee.xml" file as below.
<Employees>
<Employee type="permanent">
<Name>John</Name>
<Id>E003674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>E003675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Tom</Name>
<Id>E003676</Id>
<Age>28</Age>
</Employee>
<Employee type="permanent">
<Name>Steve</Name>
<Id>E003677</Id>
<Age>28</Age>
</Employee>
<Employee type="contract">
<Name>Bren</Name>
<Id>E003678</Id>
<Age>38</Age>
</Employee>
<Employee type="permanent">
<Name>Javier</Name>
<Id>E003679</Id>
<Age>28</Age>
</Employee>
</Employees>
protected void Page_Load(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Employee.xml"));
Grid_employee.DataSource = ds;
Grid_employee.DataBind();
}
catch (Exception)
{
throw;
}
}
But you have noticed that the Header column is eaual to the XML file so if we want to change the header name then add the columns Header in Gridview control.
<asp:GridView runat="server" ID="Grid_employee" CssClass="table table-bordered" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Employee Name" />
<asp:BoundField DataField="Id" HeaderText="Employee Id" />
<asp:BoundField DataField="Age" HeaderText="Employee Age" />
<asp:BoundField DataField="type" HeaderText="Employee Type" />
</Columns>
</asp:GridView>
You can see the Header Name has been changed. |
</> The Source Code is available in Github.com/CoreProgramm/
Summary
Post a Comment