Xml to DropDownList

Sample of xml file.

<?xml version=”1.0″ encoding=”utf-8″  ?>
<unitedstates>
    <state id=”AL” name=”AL – Alabama” />
</unitedstates>

ASP code

<asp:DropDownList ID=”ddlState” runat=”server” />

Code behind

XmlDataSource xml = new XmlDataSource();
xml.DataFile = Server.MapPath(“States.xml”);

ddlState.DataSource = xml;
ddlState.DataValueField = “id”;
ddlState.DataTextField = “name”;
ddlState.AppendDataBoundItems = true;
ddlState.DataBind();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s