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

相关推荐
csdn_aspnet4 小时前
C# 在逗号分割的字符串中判断是否包含指定字符
c#·正则·linq·string·regex·contains
宝桥南山5 小时前
Blazor Web Assembly - 体验一下Authentication State从Server共享给Client
microsoft·微软·c#·asp.net·.net·.netcore
淡海水7 小时前
02-06-历史-托管GC机制-垃圾回收如何影响数据结构选择
c#·编译·gc·机器码
longxiaozhang617 小时前
C# static:静态成员,小白也能看懂
开发语言·c#
longxiaozhang617 小时前
C#继承:让代码少写一点,偷懒的好方法
开发语言·c#
rockey62718 小时前
C#脚本引擎之AScript与DynamicExpresso对比
c#·.net·script·动态脚本
nicehuai19 小时前
烧录、查变量、看日志、调驱动:H-FlashScope搞定嵌入式下载调试的瑞士军刀
ide·vscode·visual studio
Vae_Mars1 天前
C#中的delegate委托
开发语言·c#
公子小六1 天前
基于.NET的Windows窗体编程之WinForms音频控件
windows·microsoft·c#·.net·音视频·winforms
绿浪19841 天前
c# 结构体 能不能直接封送检测
开发语言·c#