要使用任意编码加载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
好的,我应该早点想到这个. XmlTextReader(它给我们编码)和XmlReader.Create(允许我们指定编码)接受Stream.那么首先打开一个FileStream,然后将它与XmlTextReader和XmlReader一起使用,如下所示:
原文链接:https://www.f2er.com/xml/292975.htmlusing (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,encoding); using (var reader = XmlReader.Create(txtreader,context)) { return XElement.Load(reader); } } }