I have always wondered how to serialize xml to string. Actually in the last years I have implemented this functionality at least twice using different approaches. I think it's time to post the two favorite ways how to do it, just to avoid implementing this functionality once again.
Here is one way:
Here is another way:
The second way looks more elegant but it is slower. However, it can be improved by pooling transformers, which will probably make it less elegant.
The second way can be enhanced to produce a pretty xml: