aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies Read xml node values and bind data to gridview

May 8, 2012

Here I will explain how to read node values from XML document using


In previous article I explained how to insert and read data from xml in During working with XML I got requirement like read child node values from xml file and display it on webpage. My XML File Name as “SampleXML.xml” and that would be like this

<?xml version="1.0" encoding="utf-8" ?>
                             <Role>Team Leader</Role>
                             <Role>SOftware Developer</Role>
                             <Role>Business Analyst</Role>
Now I need to get node values from this xml file and bind that data to gridview for that first create xml file in your application and give name as “SampleXML.xml” and write following code in your aspx page like this 

<html xmlns="">
<head runat="server">
<title>Read XML Node values and bind data to gridview</title>
<form id="form1" runat="server">
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
After that add following namespaces in codebehind

C# Code

using System;
using System.Data;
using System.Xml;
Now add following code in code behind

protected void Page_Load(object sender, EventArgs e)
//Bind Data to Gridview
// This method is used to get xml node values and bind to gridview
protected void GetXMLData()
DataTable dt = new DataTable();
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Role", typeof(string));
XmlDocument xmldoc = new XmlDocument();
XmlNodeList nodeList = xmldoc.SelectNodes("/users/user");
foreach (XmlNode node in nodeList)
DataRow dtrow = dt.NewRow();
dtrow["FirstName"] = node["FirstName"].InnerText;
dtrow["LastName"] = node["LastName"].InnerText;
dtrow["UserName"] = node["UserName"].InnerText;
dtrow["Role"] = node["Job"]["Role"].InnerText;
gvDetails.DataSource = dt;

Imports System.Data
Imports System.Xml
Partial Class Default
Inherits System.Web.UI.Page
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 dt As New DataTable()
dt.Columns.Add("FirstName", GetType(String))
dt.Columns.Add("LastName", GetType(String))
dt.Columns.Add("UserName", GetType(String))
dt.Columns.Add("Role", GetType(String))
Dim xmldoc As New XmlDocument()
Dim nodeList As XmlNodeList = xmldoc.SelectNodes("/users/user")
For Each node As XmlNode In nodeList
Dim dtrow As DataRow = dt.NewRow()
dtrow("FirstName") = node("FirstName").InnerText
dtrow("LastName") = node("LastName").InnerText
dtrow("UserName") = node("UserName").InnerText
dtrow("Role") = node("Job")("Role").InnerText
gvDetails.DataSource = dt
End Sub
End Class

Download sample code attached

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

Get the latest,, 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


saikumar said...

Fabulous Work Brother .........Thanx for all ur posts...

Unknown said...

thanks a lot suresh!!!!
i struggle for this three days finally i get it...

Domain Hosting Expert Directory List said...

sir i want selected row form xml in to grid view

pls help me

Domain Hosting Expert Directory List said...

i also asked it last week

Anonymous said...


How to bind data in RadGrid control using Ajax/Jquery/JSON/any other method...But my real requirements is,need to bind data in a RadTextBox which is inside the RadGrid...also need to activate the RadGrid's Paging property...RadGrid may contains more then 20K data...

pls contact


Anonymous said...

this is that

Naresh Rajput said...

how to give the hyperlink in the above xml file.

Unknown said...

Hey Suresh,
Excellent Work!!
Thank you, you saved me a lot of time after i tried many not working examples

Anonymous said...

suppose one node is missed then ?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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