Fix xml handling.

I've found a problem with XML tag swapping caused by my last batch of changes; sorry!

This pull request fixes the problem and makes the tag swapping more robust.