【发布时间】:2017-09-14 13:07:47
【问题描述】:
我已经使用 XDocument 创建了简单的 xml 文档。我已经使用 XDocument 和 XDeclaration 创建了文档。
XDocument encodedDoc8 = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Root", "Content"));
如果我将此文档保存到文件中,则表示编码类型未更改。
using (TextWriter sw = new StreamWriter(@"C:\sample.txt", false)){
encodedDoc8.Save(sw);
}
输出:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>Content</Root>
但是,如果我使用 XDocument 的 WriteTo 方法打印 xml 意味着编码类型发生了变化。
using (XmlWriter writ = XmlWriter.Create(Console.Out))
{
encodedDoc8.WriteTo(writ);
}
输出:
<?xml version="1.0" encoding="IBM437" standalone="yes"?><Root>Content</Root>
为什么会这样?请更新您的答案。提前致谢。
【问题讨论】:
-
因为
Console.Out.Encoding将是IBM437