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)

Comments

 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.

Josh

 Chris

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

Chris

 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.

Nick

Mads Kristensen

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

Mads Kristensen

Cool Dude

Hey Dude Great job!!!! Thanks.... @least few people spend time to save time of many others...

Cool Dude

 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

 Shourov Bhattacharya

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.

Shourov Bhattacharya

 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.

Kelasings

Alberto

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

Alberto

jonswain.wordpress.com

Pingback from jonswain.wordpress.com XML Country List &laquo; Jonswain&#8217;s Weblog

jonswain.wordpress.com

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? Thanks!

Jon

Muzikayise

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

Muzikayise

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 &amp; Best Regards, Rizwan a.k.a RizwanSharp

RizwanSharp

Stephen

Thanks for the country list. It saved me alot of time :) All the best Mads! Stephen

Stephen

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.. lovely suresh India

suresh

Johny Goerend

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

Johny Goerend

canonprinterdriver.interactiveinfonet.info

Pingback from canonprinterdriver.interactiveinfonet.info Canon printer driver - Canon s520 - C3380i canon driver ir printer

canonprinterdriver.interactiveinfonet.info

blog.platformular.com

Pingback from blog.platformular.com Country Drop Down Html Helper for Asp.net MVC | Blog of Platformular

blog.platformular.com

Comments are closed