要使用任意编码加载XML文件,我有以下代码:
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); }
这有效,但打开文件两次似乎有点低效.是否有更好的方法来检测编码,以便我可以这样做:
1. Open file 2. Detect encoding 3. Read XML into an XElement 4. Close file
Peter Lillev.. 8
好的,我应该早点想到这个.XmlTextReader(它给我们编码)和XmlReader.Create(允许我们指定编码)都接受一个Stream.那么首先打开一个FileStream,然后将它与XmlTextReader和XmlReader一起使用,如下所示:
using (var txtreader = new FileStream(filepath, FileMode.Open)) { using (var xmlreader = new XmlTextReader(txtreader)) { // Read in the encoding info xmlreader.MoveToContent(); var encoding = xmlreader.Encoding; // Rewind to the beginning txtreader.Seek(0, SeekOrigin.Begin); 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(txtreader, settings, context)) { return XElement.Load(reader); } } }
这就像一个魅力.以独立于编码的方式读取XML文件本来应该更优雅,但至少我只打开了一个文件.
好的,我应该早点想到这个.XmlTextReader(它给我们编码)和XmlReader.Create(允许我们指定编码)都接受一个Stream.那么首先打开一个FileStream,然后将它与XmlTextReader和XmlReader一起使用,如下所示:
using (var txtreader = new FileStream(filepath, FileMode.Open)) { using (var xmlreader = new XmlTextReader(txtreader)) { // Read in the encoding info xmlreader.MoveToContent(); var encoding = xmlreader.Encoding; // Rewind to the beginning txtreader.Seek(0, SeekOrigin.Begin); 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(txtreader, settings, context)) { return XElement.Load(reader); } } }
这就像一个魅力.以独立于编码的方式读取XML文件本来应该更优雅,但至少我只打开了一个文件.