C# – Very Basic XML Parsing

In my app I have a VERY basic XML file with a list of countries & country codes. It looks like this;

<xml>
<item><code>ABW</code><country>Aruba</country></item>
<item><code>AFG</code><country>Afghanistan</country></item>
</xml>

Parsing this is very simple.
Using System.Xml;

XmlDocument xml = new XmlDocument();
xml.load("countries.xml");
XmlNodeList nodes = xml.SelectNodes("/xml/item");
foreach(XmlNode node in nodes) {
    Console.WriteLine(node["code"].InnerText + " = " + node["country"].InnerText);
}

Maybe you can use this as the basis for an RSS reader (although I’m sure there are better alternatives) .  I use it just to populate a drop-down box & convert from the country code to name.

One Response to “C# – Very Basic XML Parsing”

  1. […] Simple XML parsing – I use this to convert between county names & country codes […]

Leave a Reply to C# Snippets | piggeh.co.uk

Dansette