把xml的格式从utf-8-bom转为utf-8

1、转换方法

public static void SaveXmlWithoutBom(XmlDocument document, string filePath)

{

UTF8Encoding utf8NoBom = new UTF8Encoding(false);

XmlWriterSettings settings = new XmlWriterSettings

{

Encoding = utf8NoBom,

Indent = true,

IndentChars = " ",

NewLineChars = "\r\n"

};

using (XmlWriter writer = XmlWriter.Create(filePath, settings))

{

document.Save(writer);

}

}

2、调用

XmlDocument document = new XmlDocument();

XmlDeclaration xd = document.CreateXmlDeclaration("1.0", "UTF-8", "");

XmlElement node = null;

document.AppendChild(document.CreateElement("package"));

node = document.DocumentElement;

XmlAttribute doFlag = document.CreateAttribute("name");

node.Attributes.Append(doFlag);

document.InsertBefore(xd, node);

XmlElement elobjectEn = document.CreateElement("packageDescribe");

XmlAttribute aType = document.CreateAttribute("name");

aType.Value = "封装格式";

elobjectEn.Attributes.Append(aType);

elobjectEn.InnerText = "电子文件封装技术规范";

node.AppendChild(elobjectEn);

string filepath ="C:a.xml" ;

SaveXmlWithoutBom(document, filepath);

相关推荐
持梦远方3 小时前
【C++日志库】启程者团队开源:轻量级高性能VoyLog日志库完全指南
开发语言·c++·visual studio
聪明努力的积极向上3 小时前
【C#】HTTP中URL编码方式解析
开发语言·http·c#
一匹电信狗5 小时前
【C++】红黑树详解(2w字详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
whm27775 小时前
Visual Basic 菜单编辑器
开发语言·visual studio
关关长语5 小时前
(四) Dotnet中MCP客户端与服务端交互通知日志信息
ai·c#·mcp
小码编匠6 小时前
WPF 动态模拟CPU 使用率曲线图
后端·c#·.net
聪明努力的积极向上6 小时前
【.NET】依赖注入浅显解释
c#·.net
hixiong1237 小时前
C# OpencvSharp使用lpd_yunet进行车牌检测
开发语言·opencv·计算机视觉·c#
专注VB编程开发20年9 小时前
.net c#音频放大,音量增益算法防止溢出
算法·c#·音频处理·录音·音量增益·增益控制
专注VB编程开发20年9 小时前
.NET Reflector反编绎,如何移除DLL中的一个公开属性
开发语言·c++·c#