To load XML files with arbitrary encoding I have the following code:
Encoding encoding; using (var reader = new XmlTextReader(filepath)) { reader.MoveToContent(); encoding = reader.Encoding; } var settings = new XmlReaderSettings { NameTable = new NameTable() }; var xmlns = new XmlNamespaceManager(settings.NameTable); var context = new XmlParserContext(null, xmlns, '', XmlSpace.Default, encoding); using (var reader = XmlReader.Create(filepath, settings, context)) { return XElement.Load(reader); }
This works, but it seems a bit inefficient to open the file twice. Is there a better way to detect the encoding such that I can do:
- Open file
- Detect encoding
- Read XML into an XElement
- Close file
Ok, I should have thought of this earlier. Both XmlTextReader (which gives us the Encoding) and XmlReader.Create (which allows us to specify encoding) accepts a Stream. So how about first opening a FileStream and then use this with both XmlTextReader and XmlReader, like this:
This works like a charm. Reading XML files in an encoding independent way should have been more elegant but at least I’m getting away with only one file open.