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

Asp.net Read xml node values and bind data to gridview

May 8, 2012
Introduction:

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

Description:

In previous article I explained how to insert and read data from xml in asp.net. 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" ?>
<users>
          <user>
                   <FirstName>Suresh</FirstName>
                   <LastName>Dasari</LastName>
                   <UserName>SureshDasari</UserName>
                   <Job>
                             <Role>Team Leader</Role>
                   </Job>
          </user>
          <user>
                   <FirstName>Mahesh</FirstName>
                   <LastName>Dasari</LastName>
                   <UserName>MaheshDasari</UserName>
                   <Job>
                             <Role>SOftware Developer</Role>
                   </Job>
          </user>
          <user>
                   <FirstName>Madhav</FirstName>
                   <LastName>Yemineni</LastName>
                   <UserName>MadhavYemineni</UserName>
                   <Job>
                             <Role>Business Analyst</Role>
                   </Job>
          </user>
</users>
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="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Read XML Node values and bind data to gridview</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>
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)
{
if(!IsPostBack)
{
//Bind Data to Gridview
GetXMLData();
}
}
// 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();
xmldoc.Load(Server.MapPath("SampleXML.xml"));
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;
dt.Rows.Add(dtrow);
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
VB.NET Code


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
GetXMLData()
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()
xmldoc.Load(Server.MapPath("SampleXML.xml"))
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
dt.Rows.Add(dtrow)
Next
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
End Class
Demo

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

9 comments :

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
gopesh.porwal@gmail.com

Domain Hosting Expert Directory List said...

i also asked it last week

Anonymous said...

Hello,

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 me:santhoshnair86@gmail.com


Regards
Santhosh

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 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.