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

相关推荐
用户83562907805138 分钟前
C# 高效生成 Word 表格:复杂表格创建实战指南
后端·c#
屠夫41 分钟前
C# LINQ
c#
光头闪亮亮3 小时前
电子发票解析工具-c#桌面应用开发-DataGridView表格控件使用详解
c#
周杰伦fans4 小时前
C# 中的 `Hashtable`
开发语言·c#
lingggggaaaa4 小时前
免杀对抗——C2远控篇&PowerShell&有无文件落地&C#参数调用&绕AMSI&ETW&去混淆特征
c语言·开发语言·笔记·学习·安全·microsoft·c#
咩图4 小时前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
q***16085 小时前
Tomcat的server.xml配置详解
xml·java·tomcat
小龙报5 小时前
《算法通关指南数据结构和算法篇(2)--- 链表专题》
c语言·数据结构·c++·算法·链表·学习方法·visual studio
Charles_go5 小时前
C#中级45、什么是组合优于继承
开发语言·c#
我是唐青枫5 小时前
一文理解 C#.NET Tuples:从基础到高级应用
c#·.net