fixed wrong order in writing xml files
added support for optional tags in xml