In this article we will discuss how to
populate dropdown with xml data. It is very basic article and may be useful for
beginners.
Steps to Follow:
1.
Add
a drodown in your webpage :
<asp:DropDownList ID="ddlCountry" runat="server" />
2.
Create
a XML file (Ex. Given Below)
<?xml version="1.0" encoding="utf-8" ?>
< Countries>
<Country>
<value>1</value> <desc> India</desc></High>
<Country>
<value>2</value> <desc>USA</desc></Country>
<Country>
<value>3</value> <desc>UK</desc></Country>
<Country>
<value>4</value> <desc>China</desc></Country>
<Country>
<value>5</value> <desc>France</desc></Country>
</ Countries >
3. Now Right Code to
Bind drodown with XML data:
using System.Data;
protected void Page_Load(object sender, EventArgs e)
{
FillDropDownList();
}
private void FillDropDownList()
{
DataSet ds = new
DataSet();
ds.ReadXml(Server.MapPath("~/XmlData.xml"));
ddlCountry.DataSource = ds;
ddlCountry.DataValueField = "value";
ddlCountry.DataTextField = "desc";
ddlCountry.DataBind();
ds.Dispose();
}