问题描述
我使用C#中的序列化和反序列化我的项目(这是一个类)。
他们是序列化并保存到一个XML文件。当加载项目,一切顺利。
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.
现在我想带code中的序列化项目为Base64,然后保存文件,该文件顺利了。该文件的第一行(!EN codeD之前)看起来是这样的:
Now I'm trying to encode the serialized Project to Base64 and then save the file, which goes well too. The first line of the file (before encoded!) looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
当我去code中的文件,有一个的 的在该行前面加:
When I decode the file, there's a ? added in front of the line:
?<?xml version="1.0" encoding="utf-8"?>
<Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
在code我使用EN code:
The code I use to encode:
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
而code解码:
And the code for decoding:
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
什么能这样,我该如何解决这一问题?
What can this be and how can I fix this?
推荐答案
文件声明自己为UTF-8 - 那么,为什么您使用的ASCII为en code将其转换成二进制?有以UTF-8字符数不能被重新在ASCII psented $ P $。你甚至要在内存中的文本形式的文件开始?为什么不直接加载它作为二进制数据入手(如 File.ReadAllBytes
)?
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
)?
如果您的不的需要开始一个字符串,用 Encoding.UTF-8
(或 Encoding.Uni code
,尽管这可能会导致更大的字节数组),一切都应该罚款。这额外的字符是字节顺序标记 - 它不能重新在ASCII psented $ P $,因而有?替换字符。
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.
这篇关于C#base64编码/使用对象问题的系列化解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!