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