Complete implementation of writing the XML