gpt4 book ai didi

c# - XmlWriterSettings 对从 XmlDocument 创建的 XmlWriter 没有影响

转载 作者:数据小太阳 更新时间:2023-10-29 02:43:15 25 4
gpt4 key购买 nike

我需要使用 ISO Latin 1 编码,但使用编写器设置下方的代码默认返回 UTF8。我在这里缺少什么?

XmlDocument xmlDoc = new XmlDocument(); 
XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.Encoding = System.Text.Encoding.GetEncoding(28591);
using (XmlWriter writer = XmlWriter.Create(xmlDoc.CreateNavigato().AppendChild(), settings))
{
}

最佳答案

问题在于底层流(在本例中为 xmlDoc 对象)正在使用 UTF-8,这是 .NET 中的默认编码。

来自 Encoding property 的 MSDN 文档XmlWriterSettings 的:

The Encoding property only applies to the XmlWriter instances that are created either with the specified Stream or with the specified file name. If the XmlWriter instance is created with the specified TextWriter, the Encoding property is overridden by the encoding of the underlying TextWriter. For example, if this property is set to Unicode (UTF-16) for a particular XmlWriter, but the underlying writer is an StreamWriter (which derives from TextWriter) with its encoding set to UTF-8, the output will be UTF-8 encoded.

为了解决这个问题,您需要使用 Latin-1 编码的流创建您的 XmlWriter。我认为您不能为此使用 XmlDocument

关于c# - XmlWriterSettings 对从 XmlDocument 创建的 XmlWriter 没有影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2239381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com