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

how to bind xml data to dropdownlist/gridview in

May 12, 2012

Here I will explain how to read data from xml file and bind xml data to dropdownlist or gridview in


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 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" ?>
                   <Job>Team Leader</Job>
                   <Job>Software Developer</Job>
                   <Job>Business Analyst</Job>
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="">
<head runat="server">
<title>Read Data from XML and Bind Data to gridview/dropdownlist in</title>
<form id="form1" runat="server">
<td><b>Dropdown List</b></td>
<td><asp:DropDownList ID="ddlDetails" runat="server"/></td>
<td><b>Gridview Details</b></td>
<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)
// 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();
if (ds.Tables.Count != 0)
//Bind Data to gridview
gvDetails.DataSource = ds;
//Bind Data to dropdownlist
ddlDetails.DataSource = ds;
ddlDetails.DataTextField = "UserName";
ddlDetails.DataValueField = "UserName";

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
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()
If ds.Tables.Count <> 0 Then
'Bind Data to gridview
gvDetails.DataSource = ds
'Bind Data to dropdownlist
ddlDetails.DataSource = ds
ddlDetails.DataTextField = "UserName"
ddlDetails.DataValueField = "UserName"
End If
End Sub
End Class

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


Anonymous said...


Anonymous said...

Please give an example of Bulk Insert, Bulk Update using XML file and C# which is widely used in projects in companies.

Anonymous said...

good job

Developer Knowledge House said...

All articles in this properly descriptive and elegant.

Anonymous said...

hi..i need more steps to excecute and how to save this codes.

Anonymous said...

Nice article, this helped me.

Unknown said...

i didnt get any response of my questions

Anonymous said...

nice article

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.