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

相关推荐
贾斯汀frank3 小时前
C# 15 类型系统改进:Union Types
开发语言·windows·c#
时代的狂5 小时前
如何理解 C# 的 async 和 await
c#·.netcore·async·await
蜕变的土豆6 小时前
Visual Studio编译器调试信息格式详解:PDB文件的重要性与发布环境配置指南
visual studio
xiaoshuaishuai88 小时前
C# AI实现PR处理、单元测试
开发语言·c#·log4j
LONGZHIQIN8 小时前
C#基础复习笔记
开发语言·笔记·c#
时代的狂10 小时前
如何理解 C#/.NET 的依赖注入与生命周期
c#·.net·依赖注入·控制反转
品尚公益团队10 小时前
C#在asp.net网页开发中的带cookie登录实现
前端·c#·asp.net
吴可可12310 小时前
C#自定义CAD多段线零件实体
c#
CallmeFoureyes10 小时前
使用 C# 提取 Word 文档中的表格数据
开发语言·c#·word
思麟呀11 小时前
在C++基础上理解CSharp-7
java·jvm·c++·c#