Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用文件中的数据填充第二个列表_C#_List_Serialization_Deserialization - Fatal编程技术网

C# 如何用文件中的数据填充第二个列表

C# 如何用文件中的数据填充第二个列表,c#,list,serialization,deserialization,C#,List,Serialization,Deserialization,因此,基本上我有一个登记表,当填写一个文件时,将创建并填充登记表中输入的详细信息 我正在尝试创建另一个列表,该列表将使用反序列化来填充文件中的数据,但我没有做到这一点 下面是反序列化的代码 private void Login_Load(object sender, EventArgs e) { //List<Person> pList2 = new List<Person>(); Stream s2 = File.Open

因此,基本上我有一个登记表,当填写一个文件时,将创建并填充登记表中输入的详细信息

我正在尝试创建另一个列表,该列表将使用反序列化来填充文件中的数据,但我没有做到这一点

下面是反序列化的代码

    private void Login_Load(object sender, EventArgs e)
    {
        //List<Person> pList2 = new List<Person>();
        Stream s2 = File.Open("test.bin", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        List<Person> pList2 = new List<Person>();
        bf.Deserialize(s2);
        foreach (Person p in pList2)
        {
            bf.Deserialize(s2);
        }
        s2.Close();

    }
private void登录\u加载(对象发送方,事件参数e)
{
//List pList2=新列表();
流s2=File.Open(“test.bin”,FileMode.Open);
BinaryFormatter bf=新的BinaryFormatter();
List pList2=新列表();
bf.反序列化(s2);
foreach(pList2中的人员p)
{
bf.反序列化(s2);
}
s2.Close();
}

看起来您没有保存反序列化中的数据。可能需要将其编辑为类似这样的内容

var pList2 = (List<Person>) bf.Deserialize(s2);
var pList2=(List)bf.反序列化(s2);
然后可以迭代
pList2
的内容

这里也是BinaryFormatter的一个优秀示例:

以下链接解释了BinaryFormatter的工作原理

请在下面找到基于该问题的更新代码块

    private void Login_Load(object sender, EventArgs e)
    {
        Stream s2 = File.Open("test.bin", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        List<Person> pList2 = (List<Person>)bf.Deserialize(s2); //Deserialize here no need to set to a new list first.

        //Loop not required for Deserialize as it was stored as a list at this point all people will be available
        //foreach (Person p in pList2)
        //{

        //}

        s2.Close();

    }
private void登录\u加载(对象发送方,事件参数e)
{
流s2=File.Open(“test.bin”,FileMode.Open);
BinaryFormatter bf=新的BinaryFormatter();
List pList2=(List)bf.Deserialize(s2);//此处反序列化无需首先设置为新列表。
//反序列化不需要循环,因为它存储为列表,此时所有人都可用
//foreach(pList2中的人员p)
//{
//}
s2.Close();
}

“无法”不是正确的问题描述。解释你期望发生的事情和实际发生的事情。您还需要显示序列化代码。您也不会对反序列化的结果执行任何操作,例如
object deserialized=bf.Deserialize(s2)
。对于我来说,pList2似乎是空的。我尝试使用注册表表单创建一个用户列表,并将其保存到文件中,然后在登录表单中创建另一个列表,该列表将通过读取文件中的详细信息来填充。要登录,用户将输入用户名和密码,程序将与文件中的详细信息进行比较。这是序列化的代码:private void btnRegister_Click(object sender,EventArgs e){pList.Add(new Person(txtboxName.Text,txtboxName.Text,(int.Parse(txtboxAge.Text)),txtboxAddress.Text,txtboxUsername.Text,txtboxPassword.Text));foreach(pList中的Person per){Stream s=File.Open(“test.txt”,FileMode.Append);BinaryFormatter bf=new BinaryFormatter();bf.Serialize(s,per);s.Close();}this.Close();}您需要解释预期会发生什么以及实际会发生什么。你的问题归结为“这不起作用”,让读者猜测到底什么不起作用。这不起作用。这与问题没有任何关系,除了“如何使用BinarySerializer”,这不是问题。我这样做了,但pList2仍然是“null”文件是否包含任何内容?您能否更新代码以反映更改?