It's very easy to tackle this problem in XSLT 2.0, but I don't know if that's an option for you. It's probably a lot less code than doing it in C#.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference