C# base64 encoding/decoding with serialization of objects issue

后端 未结 3 1402
[愿得一人]
[愿得一人] 2021-01-13 22:14

I\'m using serialization and deserialization in C# for my Project (which is a Class). They are serialized and saved to an XML file. When loading the Project, all goes well.<

3条回答
  •  囚心锁ツ
    2021-01-13 23:15

    The file declares itself as UTF-8 - so why are you using ASCII to encode it into binary? There are many characters in UTF-8 which can't be represented in ASCII. Do you even have to have the file in text form in-memory to start with? Why not just load it as binary data to start with (e.g. File.ReadAllBytes)?

    If you do need to start with a string, use Encoding.UTF-8 (or Encoding.Unicode, although that will probably result in a bigger byte array) and everything should be fine. That extra character is a byte order mark - which can't be represented in ASCII, hence the "?" replacement character.

提交回复
热议问题