DropDown List Using XML in ASP.Net C#

This .NET code implements a web page with two dropdown lists, dynamically populated from XML data.

XML for District

<?xml version="1.0" encoding="utf-8" ?>
<Districts>
    <District>
        <DistrictId>1</DistrictId>
        <DistrictName>AHMEDNAGAR</DistrictName>
    </District>
    <District>
        <DistrictId>2</DistrictId>
        <DistrictName>AKOLA</DistrictName>
    </District>
    <District>
        <DistrictId>3</DistrictId>
        <DistrictName>AMRAVATI</DistrictName>
    </District>
    <District>
        <DistrictId>4</DistrictId>
        <DistrictName>CHHATRAPATI SAMBHAJI NAGAR(AURANGABAD)</DistrictName>
    </District>
    <District>
        <DistrictId>5</DistrictId>
        <DistrictName>BEED</DistrictName>
    </District>
    <District>
        <DistrictId>6</DistrictId>
        <DistrictName>BHANDARA</DistrictName>
    </District>
    <District>
        <DistrictId>7</DistrictId>
        <DistrictName>BULDHANA</DistrictName>
    </District>
    <District>
        <DistrictId>8</DistrictId>
        <DistrictName>CHANDRAPUR</DistrictName>
    </District>
    <District>
        <DistrictId>9</DistrictId>
        <DistrictName>DHULE</DistrictName>
    </District>
    <District>
        <DistrictId>10</DistrictId>
        <DistrictName>GADCHIROLI</DistrictName>
    </District>
    <District>
        <DistrictId>11</DistrictId>
        <DistrictName>GONDIA</DistrictName>
    </District>
    <District>
        <DistrictId>12</DistrictId>
        <DistrictName>HINGOLI</DistrictName>
    </District>
    <District>
        <DistrictId>13</DistrictId>
        <DistrictName>JALGAON</DistrictName>
    </District>
    <District>
        <DistrictId>14</DistrictId>
        <DistrictName>JALNA</DistrictName>
    </District>
    <District>
        <DistrictId>15</DistrictId>
        <DistrictName>KOLHAPUR</DistrictName>
    </District>
    <District>
        <DistrictId>16</DistrictId>
        <DistrictName>LATUR</DistrictName>
    </District>
    <District>
        <DistrictId>17</DistrictId>
        <DistrictName>MUMBAI CITY</DistrictName>
    </District>
    <District>
        <DistrictId>18</DistrictId>
        <DistrictName>MUMBAI SUBURBAN</DistrictName>
    </District>
    <District>
        <DistrictId>19</DistrictId>
        <DistrictName>NAGPUR</DistrictName>
    </District>
    <District>
        <DistrictId>20</DistrictId>
        <DistrictName>NANDED</DistrictName>
    </District>
    <District>
        <DistrictId>21</DistrictId>
        <DistrictName>NANDURBAR</DistrictName>
    </District>
    <District>
        <DistrictId>22</DistrictId>
        <DistrictName>NASHIK</DistrictName>
    </District>
    <District>
        <DistrictId>23</DistrictId>
        <DistrictName>DHARASHIV(OSMANABAD)</DistrictName>
    </District>
    <District>
        <DistrictId>24</DistrictId>
        <DistrictName>PALGHAR</DistrictName>
    </District>
    <District>
        <DistrictId>25</DistrictId>
        <DistrictName>PARBHANI</DistrictName>
    </District>
    <District>
        <DistrictId>26</DistrictId>
        <DistrictName>PUNE</DistrictName>
    </District>
    <District>
        <DistrictId>27</DistrictId>
        <DistrictName>RAIGAD</DistrictName>
    </District>
    <District>
        <DistrictId>28</DistrictId>
        <DistrictName>RATNAGIRI</DistrictName>
    </District>
    <District>
        <DistrictId>29</DistrictId>
        <DistrictName>SANGLI</DistrictName>
    </District>
    <District>
        <DistrictId>30</DistrictId>
        <DistrictName>SATARA</DistrictName>
    </District>
    <District>
        <DistrictId>31</DistrictId>
        <DistrictName>SINDHDURG</DistrictName>
    </District>
    <District>
        <DistrictId>32</DistrictId>
        <DistrictName>SOLAPUR</DistrictName>
    </District>
    <District>
        <DistrictId>33</DistrictId>
        <DistrictName>THANE</DistrictName>
    </District>
    <District>
        <DistrictId>34</DistrictId>
        <DistrictName>WARDHA</DistrictName>
    </District>
    <District>
        <DistrictId>35</DistrictId>
        <DistrictName>WASHIM</DistrictName>
    </District>
    <District>
        <DistrictId>36</DistrictId>
        <DistrictName>YAVATMAL</DistrictName>
    </District>
</Districts>

XML for Taluka

<?xml version="1.0" encoding="utf-8" ?>
<Talukas>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>101</TalukaId>
        <TalukaName>AKOLE</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>102</TalukaId>
        <TalukaName>JAMKHED</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>103</TalukaId>
        <TalukaName>KARJAT</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>104</TalukaId>
        <TalukaName>KOPARGAON</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>105</TalukaId>
        <TalukaName>NAGAR</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>106</TalukaId>
        <TalukaName>NEVASA</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>107</TalukaId>
        <TalukaName>PARNER</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>108</TalukaId>
        <TalukaName>PATHARDI</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>109</TalukaId>
        <TalukaName>RAHATA</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>110</TalukaId>
        <TalukaName>RAHURI</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>111</TalukaId>
        <TalukaName>SANGAMNER</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>112</TalukaId>
        <TalukaName>SHEVGAON</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>113</TalukaId>
        <TalukaName>SHRIGONDA</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>1</DistrictId>
        <TalukaId>114</TalukaId>
        <TalukaName>SHRIRAMPUR</TalukaName>
    </Taluka>
    <!-- AKOLA TALUKA STARTS HERE-->
    <Taluka>
        <DistrictId>2</DistrictId>
        <TalukaId>201</TalukaId>
        <TalukaName>AKOLA</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>2</DistrictId>
        <TalukaId>202</TalukaId>
        <TalukaName>AKOT</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>2</DistrictId>
        <TalukaId>203</TalukaId>
        <TalukaName>BALAPUR</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>2</DistrictId>
        <TalukaId>204</TalukaId>
        <TalukaName>BARSHITAKLI</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>2</DistrictId>
        <TalukaId>205</TalukaId>
        <TalukaName>MURTIJAPUR</TalukaName>
    </Taluka>
    <Taluka>
        <DistrictId>2</DistrictId>
       

.NET Code

<div>
    <asp:DropDownList ID="ddldist" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddldist_SelectedIndexChanged"></asp:DropDownList>
</div>

<div>
    <asp:DropDownList ID="ddltaluka" runat="server"></asp:DropDownList>
</div>

C# code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataSet dsDistrict = new DataSet();
        dsDistrict.ReadXml(Server.MapPath("District.xml"));

        ddldist.DataSource = dsDistrict;
        ddldist.DataTextField = "DistrictName";
        ddldist.DataValueField = "DistrictId";
        ddldist.DataBind();

        ListItem districtLI = new ListItem("---Select District---", "-1");
        ddldist.Items.Insert(0, districtLI);

        ListItem talukaLI = new ListItem("---Select Taluka---", "-1");
        ddltaluka.Items.Insert(0, talukaLI);
    }
}

protected void ddldist_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Request.Form[ddldist.UniqueID] != null)
    {
        string selectedDistrictId = ddldist.SelectedValue;

        if (!string.IsNullOrEmpty(selectedDistrictId) && selectedDistrictId != "-1")
        {
            DataSet dsTaluka = new DataSet();
            dsTaluka.ReadXml(Server.MapPath("Taluka.xml"));

            DataTable talukaTable = dsTaluka.Tables["Taluka"];
            DataTable filteredTalukas = talukaTable.Clone();

            foreach (DataRow row in talukaTable.Rows)
            {
                if (row["DistrictId"].ToString() == selectedDistrictId)
                {
                    filteredTalukas.ImportRow(row);
                }
            }

            ddltaluka.DataSource = filteredTalukas;
            ddltaluka.DataTextField = "TalukaName";
            ddltaluka.DataValueField = "TalukaId";
            ddltaluka.DataBind();

            ListItem talukaLI = new ListItem("---Select Taluka---", "-1");
            ddltaluka.Items.Insert(0, talukaLI);
        }
        else
        {
            ddltaluka.Items.Clear();
            ListItem talukaLI = new ListItem("---Select Taluka---", "-1");
            ddltaluka.Items.Insert(0, talukaLI);
        }
    }
}


Similar Articles