Properly dispose of XmlWriter objects

This commit fixes an issue where the complete XML output would not be written before the program exited.