Introduction:
Here
I will explain how to read data from xml file and bind xml data to dropdownlist or gridview in asp.net.
Description:
In previous articles I explained articles relating
to XML
some of those are read xml node values and bind data to gridview and how to insert
and read data from xml in asp.net. In
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>
</users>
|
Now
I need to get values from this xml file and bind that data to gridview and dropdownlist 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 Data from XML and Bind Data to gridview/dropdownlist
in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<table>
<tr>
<td><b>Dropdown List</b></td>
<td><asp:DropDownList ID="ddlDetails"
runat="server"/></td>
</tr>
<tr>
<td><b>Gridview Details</b></td>
<td>
<asp:GridView ID="gvDetails"
runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</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)
{
BindDataToGridviewDropdownlist();
}
}
// This method is used to bind
data to dropdownlist and gridview
protected void
BindDataToGridviewDropdownlist()
{
XmlTextReader xmlreader = new
XmlTextReader(Server.MapPath("Sample.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables.Count != 0)
{
//Bind Data to gridview
gvDetails.DataSource = ds;
gvDetails.DataBind();
//Bind Data to dropdownlist
ddlDetails.DataSource = ds;
ddlDetails.DataTextField = "UserName";
ddlDetails.DataValueField = "UserName";
ddlDetails.DataBind();
}
}
|
VB.NET Code
Imports System.Data
Imports System.Xml
Partial Class
VBSample
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDataToGridviewDropdownlist()
End If
End Sub
' This method is used to bind
data to dropdownlist and gridview
Protected Sub
BindDataToGridviewDropdownlist()
Dim xmlreader As New XmlTextReader(Server.MapPath("Sample.xml"))
Dim ds As New DataSet()
ds.ReadXml(xmlreader)
xmlreader.Close()
If ds.Tables.Count <> 0 Then
'Bind Data to gridview
gvDetails.DataSource = ds
gvDetails.DataBind()
'Bind Data to dropdownlist
ddlDetails.DataSource = ds
ddlDetails.DataTextField = "UserName"
ddlDetails.DataValueField = "UserName"
ddlDetails.DataBind()
End If
End Sub
End Class
|
|
Demo
|
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. |
|||
|
|||
8 comments :
hkkk
Please give an example of Bulk Insert, Bulk Update using XML file and C# which is widely used in projects in companies.
good job
All articles in this properly descriptive and elegant.
hi..i need more steps to excecute and how to save this codes.
Nice article, this helped me.
i didnt get any response of my questions
nice article
Note: Only a member of this blog may post a comment.