Fix error when generating Xml.

Fix warnings/un-escaped entities with deeply nested elements.

Fixes #3718