XML DataSource in ASP.net...?
In this article I'm trying to explain How to read data from XML DataSource and display the data in GridView control. Here we read XML Data using ReadXML method.XML is another important DataSource used for different type of Applications XML is hirearchical DataBase and .NET provides different methods to access XML in both hierarchical & reational format.
XML DataSource:
In this article I'm trying to explain How to read data from XML DataSource and display the data in GridView control. Here we read XML Data using ReadXML method. Description:
XML is another important DataSource used for different type of Applications XML is hirearchical DataBase and .NET provides different methods to access XML in both hierarchical & reational format.
XML Stands for Extensible MarkUp Language. XML Tags are not predefined we must define our own tags It is little to hard to understand. XML was designed to transport and store data with focus on what data is. Rules to Follow:
• XML Document has to be saved as .xml extension.
• Data should be present only under tags
Ex: <tag>data</tag>
• While define tags , Starting tag and ending tag should be same.
How to Access XML Data:
One of the way to access XML by using DataSet. DataSet Provides ReadXML && Write XML inorder to provide read & write the XML file.How To create XML Document:
Right Clickon your project and Choose Add new Item and then choose XML File option then give a name of that file as Employee.XML. Then design the XML file like below.
<?xml version="1.0" encoding="utf-8" ?>
<EmployeeDetails>
<Employee>
<EmployeeNumber>100</EmployeeNumber>
<EmployeeName>Naveen</EmployeeName>
<Job>.NET Develper</Job>
<Manager>Siva S.K</Manager>
<Salary>10000</Salary>
</Employee>
<Employee>
<EmployeeNumber>101</EmployeeNumber>
<EmployeeName>Sekhar</EmployeeName>
<Job>SharePoint Administrator</Job>
<Manager>Siva S.K</Manager>
<Salary>10000</Salary>
</Employee>
</EmployeeDetails>How to Read XML file in Application Wise:
After creating XML file we need to display the XML content in our Applications. For that we design page like below just simply drag and drop one GridView control and give Boundedfield names same like XML Tags.Source Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XML.aspx.cs" Inherits="XML" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="GV" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white" >
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
<Columns>
<asp:BoundField HeaderText="Employee Number" DataField="EmployeeNumber" ReadOnly="true" />
<asp:BoundField HeaderText="Employee Name" DataField="EmployeeName" />
<asp:BoundField HeaderText="Job" DataField="Job" />
<asp:BoundField HeaderText="Manager" DataField="Manager" />
<asp:BoundField HeaderText="Salary" DataField="Salary" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class XML : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
ds = new DataSet();
ds.ReadXml(MapPath("~/Employee.xml"));
GV.DataSource = ds;
GV.DataBind();
}
}OutPut: