反序列化对象时的ArgumentNullException

时间:2015-01-16 13:27:17

标签: c#

enter image description here我可以在soap格式中序列化我的对象,我在字符串变量fullPath中编写了目录但是当我尝试反序列化它时,它会显示ArgumentNullException,为什么?

//The code in form1.
Train tr = new Train();
fullPath = "D:\\file.xml";
tr.WriteToFile(fullPath);

//WriteToFile() method in Train class.
public void WriteToFile(string path)
{
    using (FileStream stream = new FileStream(path, FileMode.Create))
    {
        SoapFormatter formatter = new SoapFormatter();
        formatter.Serialize(stream, this);
     }
}

//The code in Form2
Form1 f = new Form1();
string fullPath = f.fullPath;
try
{
     using (Stream stream = File.OpenRead(f.fullPath))
     {
          SoapFormatter formatter = new SoapFormatter();
          tr = formatter.Deserialize(stream) as Train;
     }
 }
 catch (ArgumentNullException ex)
 {
      MessageBox.Show("ArgumentNullException: " + ex.Message);
 }

0 个答案:

没有答案