fix potential crash when xmlfile is closed due to an error while elements are still open
clean up xmlfile tree status immediately on errors
add error handling test case for xmlfile