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)

private void BindCountry()
   XmlDocument doc = new XmlDocument();

   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)


Comment by  Josh

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.

Comment by  Nick

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.


Comment by Cool Dude

Hey Dude Great job!!!!


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

Cool Dude

Comment by  Alan Gruskoff

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?

Alan Gruskoff

Comment by  Kelasings

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.


Comment by Jon

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?


Comment by RizwanSharp

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

Comment by suresh

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..