Monday, 22 June 2015

How to bind country list in dropdown using xml ?

Make dropdownlist : -
<asp:DropDownList ID="ddlCountry" runat="server" />

Make Country.xml : -
<?xml version="1.0" encoding="utf-8" ?>

  <CountryList>
    <country code="Afghanistan" iso="4"/>
    <country code="Albania" iso="8"/>
    <country code="Algeria" iso="12"/>
    <country code="Angola" iso="24"/>
    <country code="Anguilla" iso="660"></country>
    <country code="Antarctica" iso="10"></country>
    <country code="Antigua And Barbuda" iso="28"></country>
    <country code="Argentina" iso="32"></country>
    <country code="Armenia" iso="51"></country>
    <country code="Aruba" iso="533"></country>
  </CountryList>

Code- Behind:-
private void BindCountries()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(MapPath("Country.xml"));
            DataView dv = ds.Tables[0].DefaultView;
            dv.Sort = "code";
            ddlCountry.DataTextField = "code";
            ddlCountry.DataValueField = "iso";
            ddlCountry.DataSource = dv;
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, new ListItem("-Select-", "0"));

        }

No comments:

Post a Comment

Thanks for comments.