C#中以UTF-8编码无BOM头保存XML
c#中直接以XmlDocument.Save()
接口以UTF-8编码保存的XML文件是包含BOM头的,怎么以无BOM头UTF-8编码保存XML呢?
参考代码如下:
/// <summary>
/// 以UTF-8无BOM编码保存xml至文件。
/// </summary>
/// <param name="savePath">保存至路径</param>
/// <param name="xml"></param>
public static void SaveXmlWithUTF8NotBOM(string savePath, XmlDocument xml)
{
StreamWriter sw = new StreamWriter(savePath, false, new UTF8Encoding(false));
xml.Save(sw);
sw.WriteLine();
sw.Close();
}