我正在尝试仅使用“UTF-8”编码生成XML消息:
Dim doc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument(ds)
Dim n1 As System.Xml.XmlNode = doc.FirstChild
Dim doc2 As System.Xml.XmlDocument = New System.Xml.XmlDocument
doc2.LoadXml(n1.FirstChild.OuterXml)
doc2.InsertBefore(doc2.CreateXmlDeclaration("1.0", "utf-8", "yes"), doc2.FirstChild)
doc2.Save(outFile)
但是,每次生成文件时,编码都会列为“UTF-8签名”。我试图使用以下内容删除邮件中的前三个字符:
def xmlbytes = outFile.toString().getBytes().flatten()
xmlbytes.remove(0)
xmlbytes.remove(0)
xmlbytes.remove(0)
哪个也失败了。
是否有更有效的方法确保编码仅保留为“UTF-8”?
我没有使用xDocument所以我无法应用其他线程的解决方案。
我认为使用XDocument
令你感到困惑 - possible duplicate中详述的解决方案应该仍然适用。但要明确,
代替
doc2.Save(outFile)
尝试
using (var writer = new XmlTextWriter(outFile, new UTF8Encoding(false)))
{
doc2.Save(writer);
}