多个对象序列化和反序列化
假设有一个account类,现要求实现1.将一个account类数组中的多个对象保存到txt文件中,2.从文件中读出各对象保存到数组中。请着重写清读的时候怎么从文件中读出...
假设有一个account类,现要求实现
1.将一个account类数组中的多个对象保存到txt文件中,
2.从文件中读出各对象保存到数组中。
请着重写清读的时候怎么从文件中读出一个一个的对象。
知道用序列化和反序列化,就请大侠们从这方面教教.
麻烦用二进制序列化和反序列化,谢谢。
1楼大侠能不能再写下 展开
1.将一个account类数组中的多个对象保存到txt文件中,
2.从文件中读出各对象保存到数组中。
请着重写清读的时候怎么从文件中读出一个一个的对象。
知道用序列化和反序列化,就请大侠们从这方面教教.
麻烦用二进制序列化和反序列化,谢谢。
1楼大侠能不能再写下 展开
1个回答
展开全部
没什么好说的,写个例子给你看吧,有些代码用了VS2008的特性,如果你用的是VS2008以下的版本,自己改下吧。
using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace Demo
{
public class Account
{
public int UserID { get; set; }
public string Username { get; set; }
}
class Program
{
static void Main( string[] args )
{
Account[] accounts = {
new Account(){ UserID = 1, Username = "test1" },
new Account(){ UserID = 2, Username = "test2" },
new Account(){ UserID = 3, Username = "test3" }
};
string savePath = @"c:\XmlSerializerTest.txt";
XmlSerializer xs = new XmlSerializer( typeof( Account[] ) );
using ( TextWriter tw = new StreamWriter( savePath ) )
{
xs.Serialize( tw, accounts );
tw.Close();
using ( TextReader tr = new StreamReader( savePath ) )
{
Account[] deSerializedValue = xs.Deserialize( tr ) as Account[];
if ( deSerializedValue != null && deSerializedValue.Length > 0 )
{
for ( int i = 0; i < deSerializedValue.Length; i++ )
{
Console.WriteLine( "{0}\tUserID = {1}, Username = {2}", i, deSerializedValue[ i ].UserID, deSerializedValue[ i ].Username );
}
}
}
}
Console.ReadKey();
}
}
}
//----------二进制方式,可以使用BinaryFormatter 类来以二进制格式将对象或整个连接对象图形序列化和反序列化
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Demo
{
[Serializable]
public class Account
{
public int UserID { get; set; }
public string Username { get; set; }
}
class Program
{
static void Main( string[] args )
{
Account[] accounts = {
new Account(){ UserID = 1, Username = "test1" },
new Account(){ UserID = 2, Username = "test2" },
new Account(){ UserID = 3, Username = "test3" }
};
string savePath = @"c:\BinarySerializerTest.bin";
BinaryFormatter formatter = new BinaryFormatter();
using ( FileStream writeStream = new FileStream( savePath, FileMode.Create, FileAccess.Write ) )
{
formatter.Serialize( writeStream, accounts );
//xs.Serialize( tw, accounts );
writeStream.Close();
using ( FileStream readStream = new FileStream( savePath, FileMode.Open, FileAccess.Read ) )
{
Account[] deSerializedValue = formatter.Deserialize( readStream ) as Account[];
if ( deSerializedValue != null && deSerializedValue.Length > 0 )
{
for ( int i = 0; i < deSerializedValue.Length; i++ )
{
Console.WriteLine( "{0}\tUserID = {1}, Username = {2}", i, deSerializedValue[ i ].UserID, deSerializedValue[ i ].Username );
}
}
}
}
Console.ReadKey();
}
}
}
using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace Demo
{
public class Account
{
public int UserID { get; set; }
public string Username { get; set; }
}
class Program
{
static void Main( string[] args )
{
Account[] accounts = {
new Account(){ UserID = 1, Username = "test1" },
new Account(){ UserID = 2, Username = "test2" },
new Account(){ UserID = 3, Username = "test3" }
};
string savePath = @"c:\XmlSerializerTest.txt";
XmlSerializer xs = new XmlSerializer( typeof( Account[] ) );
using ( TextWriter tw = new StreamWriter( savePath ) )
{
xs.Serialize( tw, accounts );
tw.Close();
using ( TextReader tr = new StreamReader( savePath ) )
{
Account[] deSerializedValue = xs.Deserialize( tr ) as Account[];
if ( deSerializedValue != null && deSerializedValue.Length > 0 )
{
for ( int i = 0; i < deSerializedValue.Length; i++ )
{
Console.WriteLine( "{0}\tUserID = {1}, Username = {2}", i, deSerializedValue[ i ].UserID, deSerializedValue[ i ].Username );
}
}
}
}
Console.ReadKey();
}
}
}
//----------二进制方式,可以使用BinaryFormatter 类来以二进制格式将对象或整个连接对象图形序列化和反序列化
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Demo
{
[Serializable]
public class Account
{
public int UserID { get; set; }
public string Username { get; set; }
}
class Program
{
static void Main( string[] args )
{
Account[] accounts = {
new Account(){ UserID = 1, Username = "test1" },
new Account(){ UserID = 2, Username = "test2" },
new Account(){ UserID = 3, Username = "test3" }
};
string savePath = @"c:\BinarySerializerTest.bin";
BinaryFormatter formatter = new BinaryFormatter();
using ( FileStream writeStream = new FileStream( savePath, FileMode.Create, FileAccess.Write ) )
{
formatter.Serialize( writeStream, accounts );
//xs.Serialize( tw, accounts );
writeStream.Close();
using ( FileStream readStream = new FileStream( savePath, FileMode.Open, FileAccess.Read ) )
{
Account[] deSerializedValue = formatter.Deserialize( readStream ) as Account[];
if ( deSerializedValue != null && deSerializedValue.Length > 0 )
{
for ( int i = 0; i < deSerializedValue.Length; i++ )
{
Console.WriteLine( "{0}\tUserID = {1}, Username = {2}", i, deSerializedValue[ i ].UserID, deSerializedValue[ i ].Username );
}
}
}
}
Console.ReadKey();
}
}
}
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询