我在 C# 中为我的项目(这是一个类)使用序列化和反序列化。它们被序列化并保存到 XML 文件中。加载项目时,一切顺利。
现在我正在尝试将序列化的项目编码为 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;
这可能是什么,我该如何解决?