如果我使用XDocument.Load
解析XML文件...
var x = XDocument.Load("somefile.xml");
...没有
<?xml version="1.0" encoding="..."?>
标头...<MyRootElement>
...
</MyRootElement>
...
XDocument.Load
采用的默认编码是什么? ANSI(即系统默认的旧版语言环境)? UTF-8?还有吗我有checked the documentation,但是那里没有记录。我也查看了reference source,但是在深入了解
XmlReader
源代码之后就放弃了。 最佳答案
在内部,它是calls XmlReader.Create(string, XmlReaderSettings)
。这为它提供了与该方法调用相同的默认值。根据Jeroen's comment中的链接,从BOM表中自动检测流输入的编码(如果没有BOM表,则为default being ASCII)。
但是,根据this answer,默认编码可以通过传入TextWriter
(例如StreamWriter
)来覆盖:
StreamReader reader = new StreamReader("somefile.xml", Encoding.GetEncoding(1252));
var x = XDocument.Load(reader);
关于c# - XDocument.Load假定采用哪种默认编码?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48689504/