Improved "write_xml" method