【发布时间】:2015-08-10 17:54:29
【问题描述】:
我有这个代码。
const int maxbooks = 5;
Book[] booklist = new Book[maxbooks];
FileStream fs = File.Open(@"books.txt", FileMode.Open, FileAccess.Read);
SoapFormatter sf = new SoapFormatter();
try
{
// something here, deserializing file and assigning to the array
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
fs.Close();
}
我已经想通了(或者至少,我认为我已经想通了)如何在一个单独的程序中序列化原始的对象数组。我现在希望对其进行反序列化,并使用反序列化数据创建一个新数组。仅供参考,这是我序列化原始数组的另一个文件
Book firstbook = new Book("Jimbob", "Jimbob book", "first edition", "Jimbob publishing", "1991");
Book secondbook = new Book("Greg", "Greg book", "third edition", "Unholy publishing", "2010");
Book thirdbook = new Book("Pingu", "Pingu book", "tenth edition", "Antarctic publishing", "1897");
Book fourthbook = new Book("Patrick", "Patrick book", "seventh edition", "underwater publishing", "1991");
Book fifthbook = new Book("Sally", "Sally book", "first edition", "Wowpublishing", "2015");
const int maxbooks = 5;
Book[] booklist = new Book[maxbooks];
booklist[0] = firstbook;
booklist[1] = secondbook;
booklist[2] = thirdbook;
booklist[3] = fourthbook;
booklist[4] = fifthbook;
// writing to a file
FileStream fs = File.Open(@"books.txt", FileMode.Create, FileAccess.Write);
SoapFormatter sf = new SoapFormatter();
int bookindex = 0;
try
{
while (bookindex < maxbooks)
{
sf.Serialize(fs, booklist[bookindex]);
bookindex += 1;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
fs.Close();
}
暂时使用 SOAP 序列化。任何帮助将不胜感激。
【问题讨论】:
-
问题是?
-
"我现在正在寻找反序列化它,并使用反序列化数据创建一个新数组。"
-
可能是this 线程会帮助你。
标签: c# arrays serialization deserialization