I did not know this was possible or so easy! Just found out you can extend XSLT (so you can use bits of code in there) from C# have a look here and here for the lowdown...
This would have saved me so much time...why haven't I read about this before? Maybe I did and just forgot...what else have I forgotten!

UPDATE: You can even do it from VBScript! I still can't believe I didn't know this!