o422-4349207 info@abiletechnologies.com

how to bind xml data to dropdownlist/gridview in asp.net

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 XMLsome 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
About the author