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.
Please make sure to change the following aspects of the code when re-using:
- The path used to access the file in the
readXML(string filename, string country)
reader = XmlTextReader.Create(Application.Startup + filename, settings);
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.
public static void readCountriesRegistry()
public static string getCountriesList()
This is a deprecated function I left in the class anyway, in case someone wants to do some Frankenstein research:
private void StatesList()
Another short and useful function, if you are working with telephone numbers:
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
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!