Fix Unicode / UTF8 mixing in to_xml()

During encode(), some write operations are decorated with
encode(ExternalEncoding), but others are not.

If any unicode strings end up in the non-encoded outputs, the StringIO
buffer accumulates a mix of String and Unicode, which it can't handle.

This patch is kludgy at best -- but either something needs to be done to
the accumulated output strings (as is here, with the MaybeCodecBuffer)
or the write()'s need to be homogenized somehow.