我在将序列化字符串粘贴到.txt并将其反序列化以检索原始值时遇到了麻烦
本文关键字:原始 检索 遇到 麻烦 反序列化 字符串 序列化 txt | 更新日期: 2023-09-27 18:03:54
我有一些麻烦弄清楚如何做到这一点,因为我以前从未序列化过。我能够将字符串转换为byte[]s,然后将它们序列化为新字符串,但无法将该序列化字符串保存为文本文件,重新导入并将其转换回原始值。记住,这是一个学习项目。这是我目前所做的
private string SerializeToString(string obj)
{
if (obj == null)
return null;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
byte[] Array = ms.ToArray();
//This is where I create the txt file and write to it
File.OpenWrite(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "''Text.txt")
.Write(Array, 0, Array.Length);
ms.Close();
return Convert.ToBase64String(Array);
}
private void txtCheck_Click(object sender, RoutedEventArgs e)
{
//I uploaded the file to drop box and can successfully download it but can not figure out
//how to get the original value out of it..
string Url = "http://dl.dropbox.com/u/62170850/Text.txt";
WebClient webClient = new WebClient();
//webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
//webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
byte[] myDataBuffer = webClient.DownloadData(new Uri(Url));
string s = Convert.ToBase64String(myDataBuffer);
}
使用webClient。下载字符串,不要忘记在反序列化之前使用FromBase64String
调用将下载的字符串转换回字节数组。