Improve scoping in toXml()