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

相关推荐
曲大家4 小时前
C#生成控笔视频,完整版
c#·绘图
ajassi20006 小时前
开源 C# 快速开发(三)复杂控件
开发语言·开源·c#
WangMing_X6 小时前
C#上位机软件:2.1 .NET项目解决方案的作用
开发语言·c#
Sammyyyyy9 小时前
Go与C# 谁才更能节省内存?
java·golang·c#
Dream achiever9 小时前
7.WPF 的 TextBox 和 TextBlock 控件
开发语言·c#·wpf
爱吃小胖橘10 小时前
Unity-动画子状态机
3d·unity·c#·游戏引擎
RIDDLE!10 小时前
Visual Studio使用C++配置OpenCV环境,同时添加模板以4.12为例
c++·opencv·visual studio
大飞pkz11 小时前
【设计模式】适配器模式
开发语言·设计模式·c#·适配器模式
大飞pkz11 小时前
【设计模式】外观模式
开发语言·设计模式·c#·外观模式
Zewen PAN11 小时前
新手 Visual Studio 环境配置 详解
c++·ide·visual studio