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

相关推荐
Python大数据分析@1 天前
Python 语言有什么奇技淫巧吗?
开发语言·python·c#
Envyᥫᩣ1 天前
C#中的设计模式:构建更加优雅的代码
开发语言·c#
CHHC18801 天前
C#工作流示例(WorkflowCore)
c#·工作流
oscar9991 天前
少儿编程C++快速教程之——2. 字符串处理
开发语言·c++·c#·字符串·少儿
时光追逐者1 天前
.NET 使用 CsvHelper 快速读取和写入 CSV 文件
c#·.net·.net core·csv
INSO1 天前
查漏补缺之Polly
c#
c#上位机1 天前
wpf中资源的使用
c#·wpf
Zzz_睡不醒2 天前
JS(正则表达式)
javascript·正则表达式·c#
l1t2 天前
DuckDB新版rusty_sheet 插件测试
xml·数据库·rust·插件·xlsx·duckdb
界面开发小八哥2 天前
文档控件DevExpress Office File API v25.1新本亮点:重磅升级各类API
c#·.net·界面控件·devexpress·ui开发