XML country list

Apr 23, 2006

Today, I had to bind a list of countries to a drop down list in ASP.NET 2.0. I wanted to use an XML file to store the countries. There are more than 200 so there was not way I was going to hard code them in the HTML.

I have done this many times before, but never from an XML file. Normally I store the countries in a database, because other database tables would reference it. Today, I just wanted a list of countries with no relation to any data at all.

My plan was clear and simple. Find an XML file of countries on the internet and bind them to a drop down list. How hard could that be? After 15 minutes of searching Google and Live.com for the XML country file I gave up. It wasn’t out there.

Luckily, I have dozens of country tables in various databases I’ve built during the last couple of years, so I generated my own XML list by exporting from SQL server to a CSV file and then created a method in C# that generated the XML file from it.

For anybody else in need for an XML country list, look no further. I’ll share mine with you.

If you want to bind it to a drop down list, here’s the code for that. First add the drop down list to your page:

<asp:DropDownList runat="Server" ID="ddlCountry" />

Then use this C# code to bind the countries to the drop down list:

using System.Xml;

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
      BindCountry();
}

private void BindCountry()
{
   XmlDocument doc = new XmlDocument();
   doc.Load(Server.MapPath("countries.xml"));

   foreach (XmlNode node in doc.SelectNodes("//country"))
   {
      ddlCountry.Items.Add(new ListItem(node.InnerText, node.Attributes["code"].InnerText));
   }
}

Remember to add the countries.xml in the root of your website.

Download countries.xml (12,48 KB)

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (17) -

 Josh
Josh
4/24/2006 9:17:20 PM #

Funny, I did the same thing about a week ago. I needed to be able to package country and state data into a resource file as XML. I took a slightly different approach and actually wrote T-SQL that printed the results as formatted strings (using varchars, nothing fancy), cut and pasted the code into VS, and added my top level XML nodes. Nice to see I'm not the only one out there with the same problem, though.

 Chris
Chris
5/15/2006 1:10:18 PM #

Thanks Mads!  this was most helpful.  You saved me about 6 hours of work!!

 Nick
Nick
8/21/2006 5:08:46 PM #

Hi Mads!

Thanks a lot! Just one tiny snag: Since the sample code is in C#, the a in node.attributes needs to be upper cap.

Have a nice day.

Mads Kristensen
Mads Kristensen
8/21/2006 9:15:39 PM #

Nick, thanks for the typo tip. I've corrected it.

Cool Dude
Cool Dude
11/12/2006 5:23:38 PM #

Hey Dude Great job!!!!

Thanks....

@least few people spend time to save time of many others...

 Alan Gruskoff
Alan Gruskoff
12/20/2006 7:07:09 PM #

I aint mad at you, Mads!

I have a similar text file that this can update, though having nothing to do with anything Micro$oft.

What does the ISO integer value mean and what is it used for?

 Shourov Bhattacharya
Shourov Bhattacharya
3/5/2007 12:58:42 AM #

Thanks you Mads you've saved me a few hours of work. It's people like you who share their work that makes the development world go around. Well done.

 Kelasings
Kelasings
4/3/2007 2:00:01 PM #

I love you! You just saved me a lot of work. I had a list at my old job but I forgot to take a copy when I left. Now I needed the list again and couldn't find one to save my life. Thanks a lot for this.

Alberto
Alberto United States
9/12/2007 6:06:47 PM #

Hey Mads! thank you for sharing that, it's exactly what I needed,

Alberto

Jon
Jon United Kingdom
10/1/2007 2:56:18 PM #

Thats great, but how can you get Visual Studio to populate the listbox without this code?  Normally you can select an attribute from the XML data source, but since the countries are text it doesn't show up on the XML data source?  Could a bit of XPath or some other method be used to convert the XML so the text for each country appears as an attribute like the country/ISO codes?
Thanks!

Muzikayise
Muzikayise South Africa
10/15/2007 6:59:12 PM #

Thanks, I've been looking for this, in fact this has been more than helpful.

Tripty
Tripty Australia
12/13/2007 2:24:59 AM #

Thanks that was most helpful

RizwanSharp
RizwanSharp United States
12/17/2007 11:20:23 AM #

Hi Mads,
Thanks for sharing the country list. I was looking for the same and found it immidiately through a google search which pointed your blog.
Thanks & Best Regards,
Rizwan a.k.a RizwanSharp

Stephen
Stephen Ireland
4/15/2008 6:56:41 PM #

Thanks for the country list. It saved me alot of time Smile

All the best Mads!
Stephen

suresh
suresh India
6/2/2008 6:27:42 PM #

Hi I am SURESH. I am new to asp.net and I planned to do a social network site like Orkut using asp.net..
I am made the design now of the stuffs and I thought to make a drop down list of countries in xml as my first step.
I search google for the file and opened few pages. To my suprise many sites had your link page. A huge thanks to you whole heartedly because I came in search of xml file but also found many stuffs in asp.net in your site which is helping me a lot..

lovely
suresh
India

iLyas Osmanogullari
iLyas Osmanogullari Turkey
11/8/2008 11:01:19 PM #

Thank you Mads nice comment

Johny Goerend
Johny Goerend Luxembourg
11/17/2008 7:59:52 AM #

Thank you very much. Found your blog entry as fourth or fifth result in Google, and it really helps.

Pingbacks and trackbacks (3)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.