This article will show you how you can read xml file into
dataset and bind it to gridview
control using c#.net
in asp.net.
Some of my previous articles are as follows: XML
File Generation And Data Writing Using C#.Net In Asp.Net MVC, How
To Read XML File In DataSet And Display in DataGridview Using C#.Net, Read
XML File in DataTable and Bind to DataList In Asp.Net Using C#.Net, How
To Read XML File in DataTable Using C#.Net In Asp.Net MVC, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net MVC Using
C#.Net, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net Using C#.Net,
Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net, Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net,
Bind
Asp.net DropdownList Control by XML File Data Using DataSet in C#.Net, Bind
Asp.net ListBox Control by XML File Data Using DataSet in C#.Net.
So for this article first we will create a new asp.net application and add a folder for creating xml in it.
So for this article first we will create a new asp.net application and add a folder for creating xml in it.
Here is the ML code.
<?xml version="1.0" encoding="utf-8" ?>
<students>
<student>
<id>1</id>
<name>Student 1</name>
<address>Address 1</address>
</student>
<student>
<id>2</id>
<name>Student 2</name>
<address>Address 2</address>
</student>
<student>
<id>1</id>
<name>Student 3</name>
<address>Address 3</address>
</student>
<student>
<id>1</id>
<name>Student 4</name>
<address>Address 4</address>
</student>
</students>
|
Now we will add gridview control in it. After adding this control have a look the html code of the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm19.aspx.cs" Inherits="WebApplication2.WebForm19"
%>
<!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 in Dataset And Bind To GridView In Asp.Net Using C#.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Width="100%">
</asp:GridView>
</div>
</form>
</body>
</html>
|
Now take a look of 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.Data;
namespace WebApplication2
{
public partial class WebForm19 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
string
filepath = @"\XMLDataFile\XMLFile1.xml";
DataSet
objds = new DataSet();
objds = ReadXMLFile(filepath);
if
(objds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource =
objds.Tables[0];
GridView1.DataBind();
}
}
/// <summary>
/// Function to read xml data in dataset
/// </summary>
/// <param
name="filePath"></param>
/// <returns></returns>
private
DataSet ReadXMLFile(string fileFinalPath)
{
DataSet
objds = new DataSet();
objds.ReadXml(Server.MapPath(fileFinalPath));
return
objds;
}
}
}
|
In above code I have created a function which will read the xml file into the dataset by passing the file path into the dataset ml reader. In above code I have used server.mappath to read the xml file path. Now we have done. Run the application to check the output.
0 comments:
Please let me know your view