Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

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

Apr 22, 2015
Introduction:

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

Description:

In previous articles I explained read xml node values and bind data to gridview, change gridview row background color conditionally in asp.net, Show sum of gridview column values in footer in asp.net and many articles relating gridview and xml in asp.net using c#vb.net. Now i will explain how how to read xml file and bind xml data to gridview in asp.net using c#vb.net.

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" ?>
<users>
  <user>
    <FirstName>Suresh</FirstName>
    <LastName>Dasari</LastName>
    <UserName>SureshDasari</UserName>
    <Job>Team Leader</Job>
  </user>
  <user>
    <FirstName>Mahesh</FirstName>
    <LastName>Dasari</LastName>
    <UserName>MaheshDasari</UserName>
    <Job>Software Developer</Job>
  </user>
  <user>
    <FirstName>Madhav</FirstName>
    <LastName>Yemineni</LastName>
    <UserName>MadhavYemineni</UserName>
    <Job>Business Analyst</Job>
  </user>

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="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Read Data from XML and Bind Data to gridview in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</form>
</body>
</html>

Now add following namespaces in codebehind

C# Code


using System;
using System.Data;

Once we add namespaces now add following code in code behind


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Bind Data to Gridview
GetXMLData();
}
}
// This method is used to get xml node values and bind to gridview
protected void GetXMLData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("Sample.xml"));
gvDetails.DataSource = ds;
gvDetails.DataBind();
}

VB.NET Code


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
GetXMLData()
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()
ds.ReadXml(Server.MapPath("Sample.xml"))
gvDetails.DataSource = ds
gvDetails.DataBind()
End Sub
End Class

Demo

Read XML File & Bind Data to Gridview in Asp.net using C#, VB.NET
Download Sample Code Attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Anonymous said...

how can bind complex xml to gridview with custom view can you write article for that

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.