Posts tagged: C#

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.

C# Snippets

I am not a fan of C# really, however after being forced to use it for 1/2 of my life for the past 6 months or so, I feel have a pretty good grasp of some of the language.

There are a few snippets I have written which are possibly useful for some people;

  • Zooming, Panning, Rotating images
  • Custom alert boxes
  • Threading / Background tasks
  • Image resizing
  • Zip file open/closing
  • Dynamic form population
  • Simple XML parsing – I use this to convert between county names & country codes
  • Custom ini handler – I havn’t done this yet, but its VERY likely.
  • Dynamic DLL includes – This is currently being used to allow custom forms within my application

I might not get a chance to do all of those, but I hope to.  I’ll also link to them when they are done so feel free to bookmark this page.

There may also be some “things you may not have known” section appearing.  It is surprising what you come across when you’ve spent time in other languages for a while.

Update 16/08/10 – I was hoping to have put some of this online by now, however I am off on holiday in 2 days and have been putting together FAQ’s for my colleagues & finishing off as much as I can so that I don’t get a call while I’m away.  I’ll be posting this stuff at the beginning of September instead.

Dansette