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

相关推荐
Polevne1 小时前
C# GRPC 一元与双向流
linux·算法·c#
郝学胜-神的一滴1 小时前
C++11 工程级应用 12:编译期类型魔法,干掉重复与臃肿的代码
开发语言·数据结构·c++·软件工程·visual studio
yuzhiboyouye13 小时前
那xml对应的sql语法,列举一下
xml·数据库·sql
郝学胜_神的一滴18 小时前
C++11 工程级应用 12:编译期类型魔法,干掉重复与臃肿的代码
c++·visual studio
水饺编程21 小时前
第5章,[Win32 章节] :在平面直角坐标系中描点
c语言·c++·windows·visual studio
XiaoZhenHua9821 小时前
C#工业机器视觉Recipe配方系统设计:多型号、多相机、多工位参数如何统一管理
c#·软件架构·机器视觉·工业自动化·工业软件·recipe
fujisheng6611 天前
FUI 导航实践:拆开 Layer、History、Coverage 与 Cache 的组合语义
c#·unity3d
郝学胜_神的一滴1 天前
C++11 工程级应用 11:编译期黑魔法,告别重复烂代码
c++·visual studio
淡海水1 天前
10-05-高级-模式匹配-CSharp如何改变与数据结构的交互方式
数据结构·算法·c#·模式匹配
XiaoZhenHua981 天前
C# WinForms + 西门子S7 + SQLite + EPPlus生产数据采集系统架构设计
系统架构·sqlite·c#