Thursday, April 23, 2015

Read XML File & Bind Data to Gridview in using C#, VB.NET

Here I will explain how to read xml file and bind xml data to gridview in using c#, or read and bind data to gridview from xml file in using c#,

In one situation I got requirement like read data from xml file and display it on webpage. My XML File Name as “Sample.xml” and that would contains data like this

xml version="1.0" encoding="utf-8" ?>
    <FirstName>Asma </FirstName>
    <Job>Project Manager</Job>
    <Job>Software Developer</Job>
    <Job>Business Analyst</Job>

Now I need to get values from this xml file and bind that data to gridview for that write following code in your aspx page like this.

<html xmlns="">
<head id="Head1" runat="server">
<title>Read Data from XML and Bind Data to gridview in</title>
<form id="form1" runat="server">
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />

Add below code in codebehind file
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
//Bind Data to Gridview
// This method is used to get xml node values and bind to gridview
protected void GetXMLData()
DataSet ds = new DataSet();
gvDetails.DataSource = ds;

Imports System.Web.UI.WebControls
Imports System.Data
Partial Class VBCode
Inherits System.Web.UI.Page
Private attempts As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
'Bind Data to Gridview
End If
End Sub
' This method is used to get xml node values and bind to gridview
Protected Sub GetXMLData()
Dim ds As New DataSet()
gvDetails.DataSource = ds
End Sub
End Class

Download Sample code from below button. Thank you

No comments:

Post a Comment