我正在使用C#中的序列化和反序列化来处理我的项目(这是一个类)。它们被序列化并保存到XML文件中。加载项目时,一切都很顺利。
现在我想将序列化的项目编码为Base64,然后保存文件,这也很顺利。文件的第一行(在编码之前!)看起来像这样:
当我解码文件时,在该行前面添加了一个?。
我使用的编码代码如下:
这是什么问题,我该如何解决?
(Translated and polished)
现在我想将序列化的项目编码为Base64,然后保存文件,这也很顺利。文件的第一行(在编码之前!)看起来像这样:
<?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">
当我解码文件时,在该行前面添加了一个?。
?<?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">
我使用的编码代码如下:
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
解码的代码如下:
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
这是什么问题,我该如何解决?
(Translated and polished)