Fix XML parsing so that all valid encodings are supported

Implemented requested changes (from #140), I hope I haven't missed anything.