Populate Bind DropDownList From XML File In Asp.Net

1:42:00 am 0 Comments

This Example explains how to Populate Or Bind DropDownList With XML Data From File In Asp.Net Using C# And VB.

Populate Bind DropDownList From XML File
Place one DropdownList on the page.

<asp:DropDownList ID="DropDownList1" 
                  runat="server" 
                  onselectedindexchanged
  ="DropDownList1_SelectedIndexChanged">


XML Data Can be in various formats, i'll use 3 different ones shown below.


Sample 1.
   1:  <?xml version="1.0" encoding="utf-8" ?>
   2:  <Employees>
   3:    <Detail>
   4:      <ID>1</ID>
   5:      <Name>Csharp</Name>
   6:    </Detail>
   7:    <Detail>
   8:      <ID>2</ID>
   9:      <Name>AspNet</Name>
  10:    </Detail>
  11:    <Detail>
  12:      <ID>3</ID>
  13:      <Name>Articles</Name>
  14:    </Detail>
  15:  </Employees>

Another format can be
   1:  <?xml version="1.0" encoding="utf-8" ?>
   2:  <Employees>
   3:    <Detail ID="1" Name="Csharp"></Detail>
   4:    <Detail ID="2" Name="AspNet"></Detail>
   5:    <Detail ID="3" Name="Articles"></Detail>
   6:  </Employees>

Writing following code in Page_Load event is enough to bind dropdownlist from these types of XML data.

C#
01using System.Data;
02using System.Xml;
03protected void Page_Load(object sender, EventArgs e)
04{
05        DataSet dsXml = new DataSet();
06        dsXml.ReadXml(Server.MapPath("~/XMLFile2.xml"));
07        DropDownList1.DataSource = dsXml;
08        DropDownList1.DataTextField = "Name";
09        DropDownList1.DataValueField = "ID";
10        DropDownList1.DataBind();
11        DropDownList1.AutoPostBack = true;
12}

VB.NET
1Protected Sub Page_Load(sender As Object, e As EventArgs)
2 Dim dsXml As New DataSet()
3 dsXml.ReadXml(Server.MapPath("~/XMLFile2.xml"))
4 DropDownList1.DataSource = dsXml
5 DropDownList1.DataTextField = "Name"
6 DropDownList1.DataValueField = "ID"
7 DropDownList1.DataBind()
8 DropDownList1.AutoPostBack = True
9End Sub


If XML format is mix of above two.
   1:  <?xml version="1.0" encoding="utf-8" ?>
   2:  <Details>
   3:    <Name ID="1">Amit</Name>
   4:    <Name ID="2">Jain</Name>
   5:    <Name ID="3">Csharp</Name>
   6:    <Name ID="4">AspNet</Name>
   7:    <Name ID="5">Articles</Name>
   8:  </Details>

We need to loop through nodes to fine value of ID attribute.

C#
01protected void Page_Load(object sender, EventArgs e)
02    {
03        if (!IsPostBack)
04        {
05            XmlDocument doc = new XmlDocument();
06            doc.Load(Server.MapPath("~/XMLFile.xml"));
07            XmlNodeList list = doc.GetElementsByTagName("Name");
08            foreach (XmlNode node in list)
09            {
10                ListItem lItem = new ListItem(node.InnerText, node.Attributes["ID"].Value);
11                DropDownList1.Items.Add(lItem);
12            }
13            DropDownList1.Items.Insert(0, "--Select--");
14            DropDownList1.SelectedIndex = 0;
15            DropDownList1.AutoPostBack = true;
16        }
17    }

VB
01Protected Sub Page_Load(sender As Object, e As EventArgs)
02 If Not IsPostBack Then
03  Dim doc As New XmlDocument()
04  doc.Load(Server.MapPath("~/XMLFile.xml"))
05  Dim list As XmlNodeList = doc.GetElementsByTagName("Name")
06  For Each node As XmlNode In list
07   Dim lItem As New ListItem(node.InnerText, node.Attributes("ID").Value)
08   DropDownList1.Items.Add(lItem)
09  Next
10  DropDownList1.Items.Insert(0, "--Select--")
11  DropDownList1.SelectedIndex = 0
12  DropDownList1.AutoPostBack = True
13 End If
14End Sub


To Bind DropDownList in 3 tier architecture environment, add to new class files in App_Code folder of application and name them DataLayer.cs and BusinessLayer.cs

Write Following code in these class respectively.

Data Access Layer (DAL)
01using System.Web;
02using System.Xml;
03public class DataLayer
04{
05 public DataLayer()
06 {
07 }
08    public XmlDocument GetXmlData()
09    {
10        XmlDocument doc = new XmlDocument();
11        doc.Load(HttpContext.Current.Server.MapPath("~/XMLFile.xml"));
12        return doc;
13    }
14}

Business Access Layer (BAL)
01using System.Web.UI.WebControls;
02using System.Xml;
03public class BusinessLayer
04{
05 public BusinessLayer()
06 {
07    }
08    public ListItemCollection BindDropDownList()
09    {
10        ListItemCollection ddlItems = new ListItemCollection();
11        DataLayer objDAL = new DataLayer();
12        XmlNodeList list = objDAL.GetXmlData().GetElementsByTagName("Name");
13        foreach (XmlNode node in list)
14        {
15           ListItem item = new ListItem(node.InnerText, node.Attributes["ID"].Value);
16           ddlItems.Add(item);
17 
18        }
19        return ddlItems;
20    }
21}

Write code in Page_Load event of aspx page (Presentation Layer)
01protected void Page_Load(object sender, EventArgs e)
02    {
03        if (!IsPostBack)
04        {
05            BusinessLayer objBAL = new BusinessLayer();
06            DropDownList1.DataSource = objBAL.BindDropDownList();
07            DropDownList1.DataBind();
08            DropDownList1.Items.Insert(0, "--Select--");
09            DropDownList1.SelectedIndex = 0;
10            DropDownList1.AutoPostBack = true;
11        }
12    }

0 comments: