将XmlDocument保存到文件时如何避免编写BOM?

问题描述 投票:0回答:1

我从WCF中读取XML,我需要将XML保存到磁盘上,但是保存时会出现BOM字符。我不希望包含此字符,因此我尝试了多种方式来编写没有该字符的文档,但是它不起作用,我不知道出什么问题了。有什么主意吗?

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(vReply.xmlDocumento);

////-------------------------- Save without BOM  -----------------------

var bytes = Encoding.UTF8.GetBytes(vReply.xmlDocumento);
var stream = new MemoryStream(bytes);

XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UTF8Encoding(false); // The false means, do not emit the BOM.
using (XmlWriter w = XmlWriter.Create("c://temp/my.xml", settings))
{
    xmlDoc.Save(w);
}
c# xml xmldocument byte-order-mark
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.