banner Hi, this is Shiva Kumar. You can call me Shiva. I'm a developer like you, with 7 Years of experience on different technologies. Earlier I used to write articles for Computers Today. Now I started this web site to help each other ( or ) atleast contribute some thing from my end. I'm always a student, if you need any articles or help which is not there in this web site, please send me a mail. I will update you as soon as I can.
Jul
5th

How to Popluate a DropDownList Controls with XML files

Author: admin | Files under Asp.net, tutorials

Introduction

eXtended Markup Language(XML) is a specification for create custom markup languages. In simple, xml allows you to define our own tags to represent the data in what ever way we want. Below is the simple XML file,
Countries.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <details>
  3. <countries>
  4. <countryname>USA</countryname>
  5. </countries>
  6. <countries>
  7. <countryname>China</countryname>
  8. </countries>
  9. </details>
  10. ………
  11. ………………

The xml file contains countries information “country name” within the tag. We use this xml file thru out this tutorial. Populating the DropDownList control using xml files can be done in different ways in this tutorial, I will try explain the following ways populate it:

  • Using the Xml Data Source control
  • Using System.XML namespace methods

Using the Xml Data Source control

This is very simple, create a DropDownlist and XmlDatasource controls and just bind them. I will try to give the detailed steps of how to do this:

  • Drop a DropDownlist control onto your ASP page.
  • Drop an XML datasource control onto your ASP page.
  • Copy the above countries.xml file into your project (preferably into your app_data folder).
  • Click on the XML datasource smart tag (the small arrow) and wire-up the datasource to your XML File Note: You have to do a refresh on your project files by Right Clicking on your folder or Root Folder in the solution explorer followed by refresh before the XML file will been seen by the XML datasource.
  • Bind the DropDownlist control datasource to your XML datasource
  • Enjoy, we have done our job!!

Using System.XML namespace methods

I like doing the things programmatically, now, the question is how to populate the DropDownList control from the xml file programmatically. There is name space called System.XML which helps us to parse and work with the XML files. Now, first see the example:

  1.  
  2. XmlDocument doc = new XmlDocument();
  3.  
  4. doc.Load(Server.MapPath("countries.xml"));
  5.  
  6. XmlNodeList nodeList = doc.SelectNodes("details/countries");
  7.  
  8. foreach(XmlNode node in nodeList)
  9. DropDownList1.Items.Add(new ListItem(node.SelectSingleNode("countryname").InnerText));
  10.  
  11.  

The above code snippet is easy to understand. First we are creating a Xmldocument instance and loaded it with the Countries.xml file. Now, we want to parse the XML file and get the “country names”, to do this, we need nodelist. The for each loop iterates thru the nodelist’s and populates the DropDownList control by adding them as items to it.

Similar Posts - 90% of Users have seen these posts also

Post a Comment