我是这样解决的:
用ArrayList放入多个对象(有时用ArrayList []);然后将ArrayList序列化。如你上面程序,你再建一个ArrayList c,c.Add(l);c.Add(k);
或者
ArrayList c []=new ArrayList [2];
for(int i=0;i<2;i++)
c[i]=new ArrayList();c[0]=l;
c[1]=k;但都是:b.Serialize(s, c);

解决方案 »

  1.   

    摘要:为什么要使用序列化?最重要的两个原因是:将对象的状态保存在存储媒体中以便可以在以后重新创建出完全相同的副本;按值将对象从一个应用程序域发送至另一个应用程序域。例如,序列化可用于在 ASP.NET 中保存会话状态,以及将对象复制到 Windows 窗体的剪贴板中。它还可用于按值将对象从一个应用程序域远程传递至另一个应用程序域。我是这样想的:比如一个处理用户信息的类,第一次一个用户填了一些内容,把值传给类的属性,然后把类序列化到文件11,下一次又一个用户填了其他内容,把值传给类的属性,同样序列化到文件11,可是却覆盖了11的原内容??
    不对之处,见笑了。
    还有,大家到底用序列化作什么呀??可以结合自己做过的东西介绍一下吗??谢谢!!!!
      

  2.   

    yes, you couldStream s1 = File.Open("11.bin", FileMode.Open, 
    FileAccess.Write);
    ===>
    Stream s1 = File.Open("11.bin", FileMode.Append);
      

  3.   

    to saucer:谢谢
    to AhBian:我也想问如何反序列化这些对象中的特定一个??
      

  4.   

    http://expert.csdn.net/Expert/topic/984/984383.xml?temp=.6968958
      

  5.   

    “不过可以在文件头上建立一个自己的索引,记录下每次序列化前后的文件大小,这样就可以方便的找到对象的起始位置了。
    .NET的书只能告诉你如何做Serialization,不会告诉你真么建索引表的,要看操作系统。实际上就是在你自己的文件里实现一个超级简化的FAT表,不支持插入,不支持随机删除...”
    哦,好像很烦。
    这样,把源文件反序列化,捡出想要的元素,重新序列化覆盖源文件,好像也很烦,不过应该不难实现。
    其实,我最想知道序列化的用途?什么自定义序列化啊,xml序列化啊,各抒己见,谈谈自己的应用心得。
    谢谢各位了,18:00之前结贴。
      

  6.   

    ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconserialization.htm多个对象序列化到一个文件,象你这种是无法实现的,可以用序列化 ArrayList数组
      

  7.   

    to dragon2002:我哪一种无法实现啊??请说明啊
      

  8.   

    ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconserialization.htm
     
    需要你安装vs的帮助。
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpovrserializingobjects.htm
      

  9.   

    我的意思指你写入文件的 方式:
    运行下面两个代码,结果相同:
     代码一:
     ArrayList l = new ArrayList();
    for (int x=1; x< 5; x++) 
    {
    Console.WriteLine (x);
    l.Add (x);

    Stream s = File.Open("11.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite,FileShare.ReadWrite);
    BinaryFormatter b = new BinaryFormatter();
    b.Serialize(s, l);
    s.Close();
    ArrayList k = new ArrayList();
    for (int x=6; x< 10; x++) 
    {
    Console.WriteLine (x);
    k.Add (x);

        b = new BinaryFormatter();
    Stream s1 = File.Open("11.bin", FileMode.Append);
    b.Serialize(s1, k);
    s.Close();
    Console.Write ("正在从磁盘反序列化对象图..");
    Stream r = File.Open("foo.bin", FileMode.Open, FileAccess.Read);
    BinaryFormatter c = new BinaryFormatter();
    ArrayList p = (ArrayList) c.Deserialize(r);
    Console.WriteLine ("完成。");
    foreach (int i in p) 
    {
    Console.WriteLine (i);
    }
    r.Close();
    Console.WriteLine ("\r\n按 Return 键退出。");
    Console.Read();代码二:这是我的思维方式:
         Console.WriteLine ("创建对象图");
    ArrayList test=new ArrayList();
    ArrayList l = new ArrayList();
    for (int x=0; x< 10; x++) 
    {
    Console.WriteLine (x);
    l.Add (x);
    } // 结束 for
    test.Add(1);
    l = new ArrayList();
    for ( int x=10; x< 20; x++) 
    {
    Console.WriteLine (x);
    l.Add (x);
    } // 结束 for
    test.Add(1);
    Console.Write ("正在将对象图序列化到磁盘..");
    Stream s = File.Open("foo.bin", FileMode.Create, FileAccess.ReadWrite);
    BinaryFormatter b = new BinaryFormatter();
    b.Serialize(s, test);
    s.Close();
    Console.WriteLine ("完成。");
    Console.Write ("正在从磁盘反序列化对象图..");
    Stream r = File.Open("foo.bin", FileMode.Open, FileAccess.Read);
    BinaryFormatter c = new BinaryFormatter();
    ArrayList p = (ArrayList) c.Deserialize(r);
    Console.WriteLine ("完成。");
        ArrayList ps=new  ArrayList();
    for(int t=0;t<p.Count ;t++)
    {//没有把两个arraylist取出来。
    ps.Add(p[t]);
    foreach (int i in ps) 
    {
    Console.WriteLine (i);
    }
    }
    r.Close();
    Console.WriteLine ("\r\n按 Return 键退出。");
    Console.Read();
      

  10.   

    to dragon2002:太客气了,给了这么多代码,和poetc() 的意思一样啊。
    看来是我例子没举好,谢谢了!!