Filling DropDownList with Xml file
By AzamSharp
Views: 3081

Introduction:

In one of my previous articles we saw how we can make a menu using XML file Creating Menu Using XML file. In this article we will see that how we can populate a DropDownList control with XML file as a source.

XML FILE: 

Our XML file looks something like below which simply contains the name of the clients.

<?xml version="1.0" encoding="utf-8" ?>

<names>

<name>

<client>hank</client>

</name>

<name>

<client>corry</client>

</name>

<name>

<client>david</client>

</name>

<name>

<client>james</client>

</name>

</names>

Filling a DropDownList with XML file:

Now we want to fill the DropDownList with the contents contained in the XML file. Don't forget to include the namespace System.XML.

XmlDocument doc = new XmlDocument();

doc.Load(Server.MapPath("Menu.xml"));

XmlNodeList nodeList = doc.SelectNodes("names/name");

foreach(XmlNode node in nodeList)

DropDownList1.Items.Add(new ListItem(node.SelectSingleNode("client").InnerText));

 

All we are doing is making an object of the XmlDocument class. Than we read the XML file, dig down in the nodes and selects the nodes that we want. And finally add those node's inner text to the DropDownList items.

I hope you liked the article, happy coding.

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Dropdown list
Name: pinakin
Date: 4/9/2008 11:57:56 PM
Comment:
it' simply rocks........
Subject: Filling DropDownList with Xml file
Name: Jamie Light
Date: 6/4/2008 8:34:39 AM
Comment:
Thanks for the article Azam. It saved me a bunch of time. I passed it on to others already.
Subject: Filling DropDownList with Xml file
Name: xander
Date: 7/8/2008 4:42:48 AM
Comment:
gud 1 dude



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008