Optimize the memory handling with xml writer.  See #163