将文件反序列化为List C#(Deserializing a file into List C#)
我正在尝试将包含对象的文件反序列化为对象列表,但对于某些文件,列表仅对此处的对象是我的代码:
using System; using System.Collecti.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace Test { class Program { static void Main(string[] args) { user lastuser = new user("First name", "Last name", "Username", "Password"); FileStream ToStream = new FileStream("", FileMode.OpenOrCreate); BinaryFormatter Ser = new BinaryFormatter(); List<user> ToUsers = new List<user>(); try { ToUsers = (List<user>)Ser.Deserialize(ToStream); // this is to deserialize everything in the file to the list ToUsers.Add(lastuser); // here we are adding our object (which is lastuser) to the list Ser.Serialize(ToStream, ToUsers); // here we are serializing the list back to the file } catch (System.Runtime.Serialization.SerializationException) {//this is to catch the exception if the file was empty and there is nth to deserialize to the list ToUsers.Add(lastuser); Ser.Serialize(ToStream, ToUsers); } ToStream.Close(); Cole.WriteLine("ToUsers objects : " + ToUsers.Count()); // this is to see how many objects does the list have } } }这是我正在序列化的类:
using System; using System.Collecti.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.Serialization; namespace Test { [Serializable] class user { private string Fname, Lname, Username, Password; public user() { } public user(string Fname, string Lname, string Username, string Password) { this.Fname = Fname; this.Lname = Lname; this.Username = Username; this.Password = Password; } public string GetUsername() { return Username; } } }当我运行它时,我得到列表的计数是1。
再次运行我得到它2。
运行1000次 ,你将得到2。
我知道有什么不对的,所以请帮帮我。
I'm trying to deserialize a file which contains objects into a list of objects but for some how the list takes to objects only here is my code:
using System; using System.Collecti.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace Test { class Program { static void Main(string[] args) { user lastuser = new user("First name", "Last name", "Username", "Password"); FileStream ToStream = new FileStream("", FileMode.OpenOrCreate); BinaryFormatter Ser = new BinaryFormatter(); List<user> ToUsers = new List<user>(); try { ToUsers = (List<user>)Ser.Deserialize(ToStream); // this is to deserialize everything in the file to the list ToUsers.Add(lastuser); // here we are adding our object (which is lastuser) to the list Ser.Serialize(ToStream, ToUsers); // here we are serializing the list back to the file } catch (System.Runtime.Serialization.SerializationException) {//this is to catch the exception if the file was empty and there is nth to deserialize to the list ToUsers.Add(lastuser); Ser.Serialize(ToStream, ToUsers); } ToStream.Close(); Cole.WriteLine("ToUsers objects : " + ToUsers.Count()); // this is to see how many objects does the list have } } }and this is the class I'm serializing:
using System; using System.Collecti.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.Serialization; namespace Test { [Serializable] class user { private string Fname, Lname, Username, Password; public user() { } public user(string Fname, string Lname, string Username, string Password) { this.Fname = Fname; this.Lname = Lname; this.Username = Username; this.Password = Password; } public string GetUsername() { return Username; } } }When I run it, I get the count of list is 1.
Run it again I get it 2.
Run it 1000 times and you will get 2.
I know there is something wrong so please help me.
最满意答案
你在try中的代码是
try { ToUsers = (List<user>)Ser.Deserialize(ToStream); ToUsers.Add(lastuser); Ser.Serialize(ToStream, ToUsers); }在上面的代码中发生的是,在反序列化期间,流位置指针被移动到文件的末尾。 因此,当您序列化后,包含两个对象的列表将附加在文件的末尾。
因此,新的文件结构就像
+---------------------+-----------------------------------------------------+ | List (1 user info) | List (2 user's info) | +---------------------+-----------------------------------------------------+因此,当您下次反序列化时,您再次获得包含一个用户详细信息的列表。
要覆盖现有数据,请使用将流位置指针重置为文件的开头
ToStream.Seek(0, SeekOrigin.Begin);
因此,你的try块看起来像
try { ToUsers = (List<user>)Ser.Deserialize(ToStream); ToStream.Seek(0, SeekOrigin.Begin); ToUsers.Add(lastuser); Ser.Serialize(ToStream, ToUsers); }Your code within try is
try { ToUsers = (List<user>)Ser.Deserialize(ToStream); ToUsers.Add(lastuser); Ser.Serialize(ToStream, ToUsers); }what is happening in the above code is, during de-serialization, the stream position pointer is moved to the end of the file. So when you serialize back, the list containing the two objects is appended at the end of the file.
Hence, the new file structure is like
+---------------------+-----------------------------------------------------+ | List (1 user info) | List (2 user's info) | +---------------------+-----------------------------------------------------+So, when you de-serialize the next time, you again get the list containing one user's details.
To overwrite the existing data, reset the stream position pointer to the beginning of the file using
ToStream.Seek(0, SeekOrigin.Begin);
Hence, your try block would look like
try { ToUsers = (List<user>)Ser.Deserialize(ToStream); ToStream.Seek(0, SeekOrigin.Begin); ToUsers.Add(lastuser); Ser.Serialize(ToStream, ToUsers); }#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 19 条评论) |
本站网友 高桥新城房价 | 12分钟前 发表 |
ToUsers); // here we are serializing the list back to the file } catch (System.Runtime.Serialization.SerializationException) {//this is to catch the exception if the file was empty and there is nth to deserialize to the list ToUsers.Add(lastuser); Ser.Serialize(ToStream | |
本站网友 小户型客厅装修效果图大全2013图片 | 13分钟前 发表 |
SeekOrigin.Begin); Hence | |
本站网友 赵章光101 | 8分钟前 发表 |
Username | |
本站网友 小升初网上报名 | 21分钟前 发表 |
ToUsers); } what is happening in the above code is | |
本站网友 运鸿集团 | 14分钟前 发表 |
I get the count of list is 1. Run it again I get it 2. Run it 1000 times and you will get 2. I know there is something wrong so please help me. 最满意答案 你在try中的代码是 try { ToUsers = (List<user>)Ser.Deserialize(ToStream); ToUsers.Add(lastuser); Ser.Serialize(ToStream | |
本站网友 平井一夫 | 16分钟前 发表 |
string Lname | |
本站网友 孝感学校 | 2分钟前 发表 |
列表仅对此处的对象是我的代码: using System; using System.Collecti.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace Test { class Program { static void Main(string[] args) { user lastuser = new user("First name" | |
本站网友 海城阳光医院 | 24分钟前 发表 |
the stream position pointer is moved to the end of the file. So when you serialize back | |
本站网友 z武器 | 20分钟前 发表 |
FileMode.OpenOrCreate); BinaryFormatter Ser = new BinaryFormatter(); List<user> ToUsers = new List<user>(); try { ToUsers = (List<user>)Ser.Deserialize(ToStream); // this is to deserialize everything in the file to the list ToUsers.Add(lastuser); // here we are adding our object (which is lastuser) to the list Ser.Serialize(ToStream | |
本站网友 cc软件精选站 | 4分钟前 发表 |
using System; using System.Collecti.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.Serialization; namespace Test { [Serializable] class user { private string Fname | |
本站网友 担当精神 | 15分钟前 发表 |
"Username" | |
本站网友 okapi | 29分钟前 发表 |
流位置指针被移动到文件的末尾 | |
本站网友 西安二手 | 13分钟前 发表 |
"Username" | |
本站网友 常熟房产 | 4分钟前 发表 |
Lname | |
本站网友 永远之后 | 9分钟前 发表 |
ToUsers); } what is happening in the above code is | |
本站网友 郎溪县财政局 | 12分钟前 发表 |
在反序列化期间 | |
本站网友 下体 | 24分钟前 发表 |
Username | |
本站网友 麒麟软件操作系统 | 29分钟前 发表 |
ToUsers); // here we are serializing the list back to the file } catch (System.Runtime.Serialization.SerializationException) {//this is to catch the exception if the file was empty and there is nth to deserialize to the list ToUsers.Add(lastuser); Ser.Serialize(ToStream |