Click here to Skip to main content
13,291,503 members (69,448 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 11 Feb 2009

An Address Helper Class for a List of Countries and their States

, 11 Feb 2009
Rate this:
Please Sign up or sign in to vote.
This class and XML file can be used to implement dropdownlists for the "Country" and "State" parts of any basic address builder.


In developing an application that needed an Address Helper class, I thought it might be easy finding some of the required data to build an address, namely a list of countries and their states that we find so commonly in so many websites. It was really cool when I found the modules for getting the list of countries from the Registry. For me, being so new to Windows applications and C#, the only thing remaining was to get the states listing for each country and adding it into the code.

Since it was used in so many places, I automatically assumed that the file would be around, or maybe a database or something, that I could use to just integrate it with my application, but hey, I didn’t find any such file or database. Now, it could be that I didn’t look long enough, or maybe I missed it because I used wrong search strings, but anyway, the search turned up empty. But, I did find a listing of countries of the world, and a separate listing of the states under them, which needed to be put together.

So basically, the need, combined with the stubbornness for having such a file, made me fight the good fight and put this together. I hope it saves much time and causes less frustration than it did me for a lot of people who are searching for this.


You might want to learn the basics of XML, and an intrinsic knowledge of C# is assumed.

Using the code

The Address Helper class was developed by understanding and re-writing code samples I found online, but I have to take credit for the XML file. Please feel free to use it, but preserve the author comments at the beginning of the XML file. I had to spend a lot of time and patience on this, so you know how it is.

This class provides methods to read and convert the list of Countries found in the Microsoft Telephony Registry entry to a more wieldable format, and also methods to read and work with the XML file for getting states according to the country name passed into the function.

The most important thing here is the XML file.

I have used C# to write the code, and there may be more efficient implements, suit yourself. If you are using the code, you’ll need .NET 2.0 because the ReadToNextSibling() function may not be compatible with lesser versions.

Special notes:

Please make sure to change the following aspects of the code when re-using:

  1. The path used to access the file in the readXML function:
  2. readXML(string filename, string country) 
      //Some code                                         
      reader = XmlTextReader.Create(Application.Startup + filename, settings);
      //Some code
  3. Namespace

The code is pretty well commented, and variable names are chosen to make reading easy, so it should not be a problem. Anyway, here is a short description of the notable functions in the Address Helper class.

//This function is called once in the beginning to fill the values from the
//registry and convert it to a key value pair of string(Country name) and
//int(Country Telephone code)

public static void readCountriesRegistry()

//You can call this function from inside this class from the function

public static string[] getCountriesList()

//In which case, you might want to make the readCountriesRegistry() function
//private, or you can call the two functions one after the other in the above
//order... suit yourself.

This is a deprecated function I left in the class anyway, in case someone wants to do some Frankenstein research:


private void StatesList()

//If you want to experiment with reading from a text file, you can expand this
//Function, otherwise neglect it.

Another short and useful function, if you are working with telephone numbers:

//Send in a country, get the telephony country code :)

public static string getCountryCode(string country)

We already spoke a bit about the function readXML. It takes in a file name for the XML file containing the country-state trees.

public static string[] readXML(string filename, string country)

The XML file is pretty simple but effective. It has the root element "Countries", and many children of type "Country". Each Country further has a number of children with "state" tags. The structure is as follows:

<!DOCTYPE Countries [

 <!ELEMENT Countries (Country*)>
 <!ELEMENT Country (state*) >
 <!ELEMENT state (#PCDATA)>

So there it is! It works great now. If you do find any bugs or improvements, do let me know, and I'll gladly look into it. I hope you find it useful.

Points of interest

It was really surprising that I didn't find this, but it was a good experience. I was strictly an OO person, but this made XML another notch on my belt.

The only thing was it took an insane amount of patience to deal with the assembly of the XML file. If you see the file, you'll know :)


The code is now applicable for countries and states, but I plan to extend it to include cities when I get time. If anyone wants to continue fighting the good fight and help me out, I'll be glad!

Happy coding!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sreedevi Jagannath
Software Developer AeroSpace Systems Pvt Ltd.
India India
Developer in C, C++, JAVA, C#, HTML, XML, and still learning new things.
Smile | :)

*Everybody Lies.*

You may also be interested in...

Comments and Discussions

Generalgood work Pin
krishna241028-Mar-09 2:17
memberkrishna241028-Mar-09 2:17 
GeneralRe: good work Pin
Sreedevi Jagannath29-Mar-09 0:07
memberSreedevi Jagannath29-Mar-09 0:07 
GeneralReview your code [modified] Pin
AndyKernahan18-Feb-09 4:09
memberAndyKernahan18-Feb-09 4:09 
GeneralRe: Review your code Pin
Sreedevi Jagannath18-Feb-09 6:33
memberSreedevi Jagannath18-Feb-09 6:33 
GeneralUK Pin
RugbyLeague18-Feb-09 3:29
memberRugbyLeague18-Feb-09 3:29 
GeneralRe: UK Pin
Sreedevi Jagannath18-Feb-09 6:46
memberSreedevi Jagannath18-Feb-09 6:46 
GeneralGood work :) Pin
GLLNS18-Feb-09 1:57
memberGLLNS18-Feb-09 1:57 
GeneralRe: Good work :) Pin
Sreedevi Jagannath18-Feb-09 6:51
memberSreedevi Jagannath18-Feb-09 6:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 11 Feb 2009
Article Copyright 2009 by Sreedevi Jagannath
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid