How to Load XML Data into Dropdownlist Asp.Net C# VB.Net

Few days ago i got an email from one of my reader. He wants an article on XML to Dropdownlist. Here in this article I will explain how one can bind or populate or load XML data into a Dropdownlist control. Asp.net DataSet provides us a method named ReadXml, where we can initially load XML file. After that we can populate Dropdownlist DataTextField & DataValueField by DataSet default view table.

To do the example first add an aspx page in your project then add a Dropdownlist control. Code will be same for both C# & VB.Net. Like:

<asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>

After that add an XML file in root path like below:

<?xml version="1.0" encoding="utf-8" ?>
<Products>
  <Product>
    <ID>1</ID>
    <Name>Lux</Name>
  </Product>
  <Product>
    <ID>2</ID>
    <Name>Harpic</Name>
  </Product>
  <Product>
      <ID>3</ID>
      <Name>Dove</Name>
  </Product>
  <Product>
      <ID>4</ID>
      <Name>Sunsilk</Name>
  </Product>
  <Product>
      <ID>5</ID>
      <Name>Pentine</Name>
  </Product>
</Products>

And then under page_load event write the below code:
C# Code:

        if (!IsPostBack)
        {
            DataSet RS = new DataSet();
            RS.ReadXml(Server.MapPath("~/ProductList.xml"));

            DataView dv = RS.Tables[0].DefaultView;

            //Dataview Sorting by column name "Name" defined in XML file 
            dv.Sort = "Name";

            // Set the DataTextField and DataValueField
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "ID";

            DropDownList1.DataSource = dv;
            DropDownList1.DataBind();
        }

Note: Don’t forget to add “using System.Data;” namespace.

VB.Net Code:

        If Not IsPostBack Then
            Dim RS As DataSet = New DataSet()
            RS.ReadXml(Server.MapPath("~/ProductList.xml"))

            Dim dv As DataView = RS.Tables(0).DefaultView

            'Dataview Sorting by column name "Name" defined in XML file 
            dv.Sort = "Name"

            'Set the DataTextField and DataValueField
            DropDownList1.DataTextField = "Name"
            DropDownList1.DataValueField = "ID"

            DropDownList1.DataSource = dv
            DropDownList1.DataBind()
        End If

Now run the project. You will get an output like:

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net, XML
3 comments on “How to Load XML Data into Dropdownlist Asp.Net C# VB.Net
  1. David Blue says:

    Awesome stuff man. I was stuck on this for awhile and this also helped me create cascading picklists as well. Thanks!

  2. Michelle says:

    Hey David, mind if I ask how you did the cascading picklists ? I’m trying to do my own but without using C#, SQL, AJAX or anything, just VB, and its taking me longer than it should, so any leads would be really appreciated. Also, thanks to Bikash for posting this, it is really helpful for anyone out here. Thanks..!!

  3. sara says:

    can you help me: I want to load XML file from FileUpload, then store it in sql that type of related fild is XML ( C#);

Leave a Reply

Your email address will not be published. Required fields are marked *

     

*