HTML won't work, as libxml adds some XML header in the first line.
Furthermore use xmlIndentTreeOutput and xmlKeepBlanksDefault to fix some formatting issues.
I'm not sure there is a better way, as the API documentation is imho quite sparse and the API itself is really arcane.