XML Country List

1 10 2007

The chances are that, at some point during your development project, you’ll need to capture data from you users. Also very likely (and very useful f0r that matter) is to capture where they are from.

The first thing you need is a listbox of countries from where they can select from. To save you time and energy, there is no need to type out every single country into the listbox. This would be a real test of your geographical knowledge!

Follow these simple steps:

1. Drop a listbox control onto your ASP page.
2. Drop an XML datasource control onto your ASP page.
3. Download and copy the countries.xml file into your project (preferably into your app_data folder).
4. 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.

5. Bind the listbox to your XML datasource and voila!

The only problem with this method is that because the country code is stored as inner text within each XML node, the only values than can be shown in your listbox is the ISO code and the 2 letter country code.  Not much use to you really!  You could either change the format of this XML file or access the inner text of each node.

To do this check out Mads Kristensen’s blog. He’s provided a method to programmatically populate the listbox in ASP code behind. Check out his blog here