This question came up in the forums a little while ago, and I thought it might be interesting to do some comparisons.
I first came up with a few ways of turning an XmlDocument into an XDocument.
private
Next I whipped up a function to time these with something quick and dirty. I make sure the past activity doesn't both much in terms of leaving garbage, and I warm up the action a bit (I also warm up the Stopwatch methods, just in case).
And finally, all together:
StringBuilder
Note that the actual numbers don't matter much, as this is my laptop running a bunch of things in the background, in the debugger and whatnot, but the relative values are interesting to see.
These are the values I got (they vary a bit each run, but not by much).
Not surprisingly, avoiding the creation of a big string just to re-parse it is a big win - save the planet, use less CPU power!
Enjoy!
Can you use C# 3.0 extension methods for this to make this a bit seamless. Just a thought....
Kris, you're right, that would certainly make the conversion code more discoverable. I've queued up a post with the sample code for that so others can benefit as well.
Thanks!