gpt4 book ai didi

c# - WriteStartDocument 编写的错误 XML 编码标记

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

我已将 XMLWriter 设置为使用 UTF8 编码,但 WriteStartDocument 方法仍然写入 UTF16 标记。

这是代码的一部分:

            m_sbXML = New System.Text.StringBuilder

m_xmlWriterSettings = New System.Xml.XmlWriterSettings
With m_xmlWriterSettings
.Indent = True
.IndentChars = " "
.Encoding = System.Text.Encoding.UTF8
End With

m_xmlWriter = System.Xml.XmlWriter.Create(m_sbXML, m_xmlWriterSettings)

Call m_xmlWriter.WriteStartDocument()

应该写文档标签:

<?xml version="1.0" encoding="utf-8"?>

而是它写道:

<?xml version="1.0" encoding="utf-16"?>

最佳答案

您正在写信给 StringBuilder . .NET 中的字符串始终采用 UTF-16 编码。

如果您想创建一个 UTF-8 编码的 XML 文件,请写入 Stream .

示例:

var settings = new XmlWriterSettings
{
Indent = True,
IndentChars = " ",
Encoding = Encoding.UTF8
};

using (var stream = new MemoryStream())
using (var writer = XmlWriter.Create(stream, settings))
{
writer.WriteStartDocument();
// ...
}

关于c# - WriteStartDocument 编写的错误 XML 编码标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8600936/

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