把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);

相关推荐
zh_xuan1 小时前
Visual Studio 上传工程到github
ide·git·github·visual studio
gihigo19983 小时前
嵌入式幼儿园刷卡系统 (C#实现)
c#
qq_454245033 小时前
通用引用管理框架
数据结构·架构·c#
aq55356003 小时前
三大编程语言深度对比:C# vs 易语言 vs 汇编
开发语言·汇编·c#
光泽雨4 小时前
c# 文件编译的过程
开发语言·c#
zxy28472253014 小时前
使用正运动的仿真软件C#
c#·仿真·运动控制·正运动·无硬件
三省持敬4 小时前
异步并发的“流量警察”:在C#中使用SemaphoreSlim进行并发控制的最佳实践
c#
唐青枫5 小时前
C#.NET IL 中间码 深入解析:从 C# 编译结果到 CLR 执行链路
c#·.net
无限进步_5 小时前
【C++】多重继承中的虚表布局分析:D类对象为何有两个虚表?
开发语言·c++·ide·windows·git·算法·visual studio
xiaoshuaishuai86 小时前
C# 方言识别
开发语言·windows·c#