[英]What default encoding is assumed by XDocument.Load?
If I use XDocument.Load
to parse an XML file... 如果我使用
XDocument.Load
来解析XML文件...
var x = XDocument.Load("somefile.xml");
...which does not have a <?xml version="1.0" encoding="..."?>
header... ... 没有
<?xml version="1.0" encoding="..."?>
标题...
<MyRootElement>
...
</MyRootElement>
...what is the default encoding assumed by XDocument.Load
? ...
XDocument.Load
假设的默认编码是什么? ANSI (ie the system default legacy locale)? ANSI(即系统默认的遗留语言环境)? UTF-8?
UTF-8? Something else?
别的什么?
I have checked the documentation , but it's not documented there. 我检查了文档 ,但没有记录在那里。 I have also looked at the reference source , but gave up after getting too deep into the
XmlReader
source. 我也查看了参考源 ,但是在深入了解
XmlReader
源之后放弃了。
Internally, it calls XmlReader.Create(string, XmlReaderSettings)
. 在内部,它调用
XmlReader.Create(string, XmlReaderSettings)
。 This gives it the same default as that method call. 这使它与该方法调用具有相同的默认值。 According to the link from Jeroen's comment , the encoding for stream input is auto-detected from the BOM (the default being ASCII if there is no BOM).
根据Jeroen评论中的链接,可以从BOM中自动检测流输入的编码(如果没有BOM,则默认为ASCII )。
However, as per this answer , the default encoding can be overridden by passing in a TextWriter
(such as a StreamWriter
): 但是,根据此答案 ,可以通过传入
TextWriter
(例如StreamWriter
)来覆盖默认编码:
StreamReader reader = new StreamReader("somefile.xml", Encoding.GetEncoding(1252));
var x = XDocument.Load(reader);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.