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. |
|||
|
|||
9 comments :
Fabulous Work Brother .........Thanx for all ur posts...
thanks a lot suresh!!!!
i struggle for this three days finally i get it...
sir i want selected row form xml in to grid view
pls help me
gopesh.porwal@gmail.com
i also asked it last week
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
this is that
how to give the hyperlink in the above xml file.
Hey Suresh,
Excellent Work!!
Thank you, you saved me a lot of time after i tried many not working examples
:)
suppose one node is missed then ?
Note: Only a member of this blog may post a comment.