1
有时加载xml文件会有bom文件头,可用以下方法去除
/// <summary>
/// 去除文件bom头后的字符
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static string GetUTF8String(byte[] buffer)
{
if (buffer == null)
return null;
if (buffer.Length <= 3)
{
return Encoding.UTF8.GetString(buffer);
}
byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
if (buffer[0] == bomBuffer[0]
&& buffer[1] == bomBuffer[1]
&& buffer[2] == bomBuffer[2])
{
return new UTF8Encoding(false).GetString(buffer, 3, buffer.Length - 3);
}
return Encoding.UTF8.GetString(buffer);
}