.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Bind xml file into GridView

Posted By: Ramesh     Posted Date: January 07, 2010    Points:2   Category :ASP.Net
Hi,

I have a xml file and I need to display or bind the data contain in xml file to GridView in asp.net application.

I am using C# and Asp.Net.

Regards
Ramesh

Responses
Author: Lalij Mer             
Posted Date: January 07, 2010     Points: 5   

use this code sample
--------------------

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Data"%>

<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
DataSet authorsDataSet;
string filePath = Server.MapPath("Authors.xml");
authorsDataSet = new DataSet();
//Read the contents of the XML file into the DataSet
authorsDataSet.ReadXml(filePath);
authorsGird.DataSource = authorsDataSet.Tables[0].DefaultView;
authorsGird.DataBind();
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Reading XML Data into a DataSet object </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView id="authorsGird" runat="server"
AutoGenerateColumns="False" CellPadding="4" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White"
HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">
<Columns>
<asp:BoundField HeaderText="Last Name" DataField="lastName" />
<asp:BoundField HeaderText="First Name"
DataField="firstName" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


Another Example
---------------

XmlTextReader Reader1 = new XmlTextReader(server.MapPath("XmlFilePath.xml"));
XmlDataDocument xdoc1 = new XmlDataDocument();
xdoc1.DataSet.ReadXml(Reader1, XmlReadMode.InferSchema);

Dataset ds= xdoc1.DataSet;

Gridview1.DataSource=ds;

Gridview1.DataBind();

for details
--------------
http://forums.asp.net/t/1185276.aspx

http://www.devx.com/tips/Tip/31731


Please Check my answer if you help....
Thank You...

Lalit.
Author: Lalij Mer             
Posted Date: January 07, 2010     Points: 5   

This code sample binds XML data to a GridView control, by using DataTable.ReadXML() function.

The below code shows a simple example of binding the GridView control dynamically. The GridView control here is nested.

The code samples have .aspx page followed by the code-behind file.


<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="Grd" AutoGenerateColumns="false" OnRowDataBound="OnDataBound">
<Columns>
<asp:BoundField DataField="agent_cd" HeaderText="Agent Cd" />
<asp:TemplateField HeaderText ="Agent Details">
<ItemTemplate>
<asp:GridView ID="ChldGrid" runat="server">
<Columns>
<asp:BoundField DataField="agent_name" HeaderText="Agent Cd" />
<asp:BoundField DataField="address1" HeaderText="Address" />
<asp:BoundField DataField="city" HeaderText="City" />
<asp:BoundField DataField="state" HeaderText="State" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>

The following code is the code-behind C# file for the above .aspx page.


//Code for binding the GridView
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class NestedGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
try
{
SqlConnection conn = new SqlConnection("");
conn.Open();

DataTable dt = new DataTable();
SqlDataAdapter ADA = new SqlDataAdapter("", conn);
ADA.Fill(dt);
Grd.DataSource = dt;
Grd.DataBind();
}
catch (Exception E)
{
string str = E.Message;
}
}
protected void OnDataBound(object sender , GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView Grd = (GridView)e.Row.FindControl("ChldGrid");
DataTable dt = new DataTable("TEST");
dt.Columns.Add("agent_cd");
dt.Columns.Add("agent_name");
dt.Columns.Add("address1");
dt.Columns.Add("city");
dt.Columns.Add("state");
dt.ReadXml(Server.MapPath("Data.xml"));
Grd.DataSource = dt;
Grd.DataBind();
}
}
}



Give below is a sample XML Content that can be used to test the program. Save this content as a .xml file.


<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<TEST>
<agent_cd>00000026</agent_cd>
<agent_name>TROPIC ICE INCORPORATED</agent_name>
<address1>2805 N COMMERCE PKWY</address1>
<city>MIRAMAR</city>
<state>FL</state>
</TEST>
</DocumentElement>


Please Check my answer if you help....
Thank You...

Lalit.


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend