This article will show you how you can access read xml file
data using linq query and add in datatable to bind gridview in asp.net using
c#.net. This dataset we will bind with the gridview using c#.net.
Some of my previous articles are as follows: Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net,
Bind
Asp.net ListBox Control by XML File Data Using DataSet in C#.Net, Image
Save In XML and Load XML Bitmap Image File in Windows Application Using C#.Net,
How
to Save Record in XML File and Read XML to Display in DataGridview Using C#.net
in Windows Application, CheckBoxList
Bind By Using XmlDataSource in Asp.Net, DropDownList
Bind By Using XmlDataSource in Asp.Net, How
to Bind xml to DataGridView Using C#.Net In Windows Application.
So for this article first we will create a new asp.net application and add the below code into .aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!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>Read XML
File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net
</title>
<link href="Styles/Site.css" rel="stylesheet"
type="text/css"
/>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" Width="100%">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
After this we will create an xml file in below shown format.
xml version="1.0" encoding="utf-8" ?>
<CountryModel>
<Country>
<ID>1</ID>
<COUNTRY>India</COUNTRY>
<POPULATION>20000</POPULATION>
</Country>
<Country>
<ID>2</ID>
<COUNTRY>Pakistan</COUNTRY>
<POPULATION>30000</POPULATION>
</Country>
<Country>
<ID>3</ID>
<COUNTRY>Sri Lanka</COUNTRY>
<POPULATION>40000</POPULATION>
</Country>
<Country>
<ID>4</ID>
<COUNTRY>Nepal</COUNTRY>
<POPULATION>50000</POPULATION>
</Country>
<Country>
<ID>5</ID>
<COUNTRY>Bhutan</COUNTRY>
<POPULATION>60000</POPULATION>
</Country>
</CountryModel>
|
In above code I have added a gridview control and I will
bind it with xml data. Now add the below code into your .cs page.
using System;
using System.Linq;
using System.Data;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GetData();
}
}
public
void GetData()
{
try
{
XDocument
doc = XDocument.Load(Server.MapPath("~/XML/XMLFile1.xml"));
var
records = (from data in
doc.Root.Elements("Country")
select data);
if
(records != null)
{
DataTable
dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("COUNTRY", typeof(string));
dt.Columns.Add("POPULATION", typeof(string));
foreach
(var item in
records)
{
DataRow dr = dt.NewRow();
dr["ID"] = (string)item.Element("ID");
dr["COUNTRY"] = (string)item.Element("COUNTRY");
dr["POPULATION"] = (string)item.Element("POPULATION");
dt.Rows.Add(dr);
}
GridView1.DataSource =
dt;
GridView1.DataBind();
}
}
catch
(Exception ex)
{
}
}
}
}
|
In above code I have read the xml file using XDocument.
After reading the xml file I have read the xmldocument using linq query. After
reading the values by linq query and then added the value into datatable and
bind it to the gridviewcontrol.
Now we have done run the application and check the output.
0 comments:
Please let me know your view