Click here to Skip to main content
15,885,875 members
Articles / Programming Languages / XML

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

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
11 Feb 2009CPOL4 min read 23.4K   187   10   8
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. C#
    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.

C#
//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:

C#
//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:

C#
//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.

C#
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:

XML
<!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 :)

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)


Written By
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 Pin
krishna241028-Mar-09 1:17
krishna241028-Mar-09 1:17 
GeneralRe: good work Pin
Sreedevi Jagannath28-Mar-09 23:07
Sreedevi Jagannath28-Mar-09 23:07 
GeneralReview your code [modified] Pin
andykernahan18-Feb-09 3:09
andykernahan18-Feb-09 3:09 
GeneralRe: Review your code Pin
Sreedevi Jagannath18-Feb-09 5:33
Sreedevi Jagannath18-Feb-09 5:33 
GeneralUK Pin
RugbyLeague18-Feb-09 2:29
RugbyLeague18-Feb-09 2:29 
GeneralRe: UK Pin
Sreedevi Jagannath18-Feb-09 5:46
Sreedevi Jagannath18-Feb-09 5:46 
GeneralGood work :) Pin
GLLNS18-Feb-09 0:57
GLLNS18-Feb-09 0:57 
GeneralRe: Good work :) Pin
Sreedevi Jagannath18-Feb-09 5:51
Sreedevi Jagannath18-Feb-09 5: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.