c# Detect xml encoding from Byte Array?

前端 未结 4 1051
日久生厌
日久生厌 2020-12-17 01:38

Well i have a byte array, and i know its a xml serilized object in the byte array is there any way to get the encoding from it?

Im not going to deserilize it but im

4条回答
  •  再見小時候
    2020-12-17 02:29

    The first 2 or 3 bytes may be a Byte Order Mark (BOM) which can tell you whether the stream is UTF-8, Unicode-LittleEndian or Unicode-BigEndian.

    UTF-8 BOM is 0xEF 0xBB 0xBF Unicode-Bigendian is 0xFE 0xFF Unicode-LittleEndiaon is 0xFF 0xFE

    If none of these are present then you can use ASCII to test for (note most modern XML generation sticks to the standard that no white space may preceed the xml declare).

    ASCII is used up until ?> so you can find the presence of encoding= and find its value. If encoding isn't present or declare is not present then you can assume UTF-8.

提交回复
热议问题