Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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.

Background

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:

//IGNORE THIS FUNCTION

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*) >
 <!ATTLIST Country name CDATA #REQUIRED >
 <!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 Smile | :)

History

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!

License

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

Share

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

Comments and Discussions

 
Generalgood work Pinmemberkrishna241028-Mar-09 1:17 
GeneralRe: good work PinmemberSreedevi Jagannath28-Mar-09 23:07 
GeneralReview your code [modified] PinmemberAndyKernahan18-Feb-09 3:09 
GeneralRe: Review your code PinmemberSreedevi Jagannath18-Feb-09 5:33 
GeneralUK PinmemberRugbyLeague18-Feb-09 2:29 
GeneralRe: UK PinmemberSreedevi Jagannath18-Feb-09 5:46 
GeneralGood work :) PinmemberGLLNS18-Feb-09 0:57 
GeneralRe: Good work :) PinmemberSreedevi Jagannath18-Feb-09 5:51 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 11 Feb 2009
Article Copyright 2009 by Sreedevi Jagannath
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid