One of the simplest ways to bind data to any control is using a DataSet in Asp.Net. The DataSet, as you know, represents an in-memory cache of data retrieved from a data source, which you can bind easily with a control, such as a DropDownList.
The XML Data
Create a file and name it library.xml and copy the below data to it. Save the file. The node or tag that we are interested in is <BookName>. I’ll populate my DropDownList with the names of the books in the file.
<?xml version="1.0" standalone="yes"?> <Library> <List> <BookName>Computer Architecture</BookName> <Category>Computers</Category> </List> <List> <BookName>Advanced Composite Materials</BookName> <Category>Science</Category> </List> <List> <BookName>Asp.Net 4 Blue Book</BookName> <Category>Programming</Category> </List> <List> <BookName>Stategies Unplugged</BookName> <Category>Science</Category> </List> <List> <BookName>Teaching Science</BookName> <Category>Science</Category> </List> </Library>
In the markup section, simply add a DropDownList control inside the <body> section. It has now list items, as we are going to populate the list using a code behind procedure.
<div> <asp:DropDownList ID="ddlBooks" name="ListOfBooks" runat="server"> </asp:DropDownList> </div>
As I said in the beginning, I’ll use Asp.Net DataSet Class to extract data from the XML file and later bind the data to the DropDownList. Therefore, I’ll first need to add the namespace using System.Data (Imports System.Data for Vb) in the beginning of the code. The namespace provides all the necessary methods and properties to extract and bind data from any data source.
Next, is our data binding part. I’ll write a small procedure, which will extract the data from the XML file and bind it to the control. I’ll call this procedure from my page load event.
using System; using System.Data; // FOR DataSet. public partial class SiteMaster : System.Web.UI.MasterPage { protected void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { Populate_DropDownList(); } } // POPULATE DROPDOWNLIST USING DataSet CLASS. private void Populate_DropDownList() { using (DataSet ds = new DataSet()) { ds.ReadXml(Server.MapPath("library.xml")); ddlBooks.DataSource = ds; ddlBooks.DataTextField = "BookName"; ddlBooks.DataBind(); ddlBooks.SelectedItem.Text = "-- Select your book --"; } } }
Option Explicit On Imports System.Data ' FOR DataSet. Partial Class Site Inherits System.Web.UI.MasterPage Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Populate_DropDownList() End If End Sub 'POPULATE DROPDOWNLIST USING DataSet CLASS. Private Sub Populate_DropDownList() Using ds As New DataSet() ds.ReadXml(Server.MapPath("library.xml")) ddlBooks.DataSource = ds ddlBooks.DataTextField = "BookName" ddlBooks.DataBind() ddlBooks.SelectedItem.Text = "-- Select your book --" End Using End Sub End Class
This is a simple procedure, however, at times important when you wish to populate XML data quickly to multiple or just a single DropDownList control on your web page.
Do you know?
You can manipulate XML data using the DataSet Class in Asp.Net? I have an example just for this purpose, which shows how to extract data from XML file using DataSet and update or edit the XML data using the DataSet methods. Its simple and you must check it.