Make XML generation more robust

Don't write to the main XMLGen while creating a new record.
This avoids having to undo changes in case of an error in a really
unreliable way.
Add initial export code