Stracciafabrizio’s Weblog

Just another WordPress.com weblog

How to Populate DropDownList with Xml file

leave a comment »

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:

<root url=”./Default.aspx” title=”Home” description=”Home Page”>

<menu title=”Vendite” description=”Vendite” url=”#”>

<sottoMenu title=”Abbonamenti” description=”Vendita Abbonamenti” url=”./AcquistaAbbonamento.aspx” />

<sottoMenu title=”Multicorsa” description=”Vendita Multicorsa” url=”./AcquistaMulticorsaAll.aspx” />

</menu>

<menu title=”Storni” description=”Storno” url=”#”>

<sottoMenu title=”Abbonamento” description=”Storno Abbonamento” url=”./StornaAbbonamento.aspx” />

<sottoMenu title=”Multicorsa” description=”Storno Multicorsa” url=”./StornaMulticorsaAll.aspx” />

</menu>

<menu title=”Lista Movimenti” description=”Lista Movimenti” url=”./ListaMovimenti.aspx” />

</root>


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 from custom xml, now, the question is how to populate the DropDownList control from the xml file whit no well done XLST. There is name space called System.XML which helps us to parse and work with the XML files.

//CARICO IN MEMORIA IL FILE XML DEL TEMPLATE
XmlSourceTemplate.DataFile = pathXmlFile+”.xml”;
XmlDocument temporaneo1 = new XmlDocument();
temporaneo1.Load(@”yourPath” + pathXmlfile + “.xml”);


XmlDocument xmlApp = temporaneo1.GetXmlDocument();
XmlNodeList nodeList = xmlApp.GetElementsByTagName(“menu”);
foreach (XmlNode node in nodeList)
{

DropDownList.Items.Add(new ListItem(node.Attributes[0].Value);
}

DropDownList will be loaded with “title” attribute of xml file.


Written by stracciafabrizio

May 10, 2010 at 9:35 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: