C#如何反序列化多个对象
假设该对象只有两个值类型的简单属性,通过foreach(遍历该对象集合)进行了多次序列化,如何反序列化...
假设该对象只有两个值类型的简单属性,通过foreach(遍历该对象集合)进行了多次序列化,如何反序列化
展开
2个回答
展开全部
没什么好说的,写个例子给你看吧,有些代码用了VS2008的特性,如果你用的是VS2008以下的版本,自己改下吧。
using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace Demo
{
public class Account
{
public int UserID
public string Username
}
class Program
{
static void Main( string[] args )
{
Account[] accounts = {
new Account(),
new Account(),
new Account()
};
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( "\tUserID = , Username = ", 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
public string Username
}
class Program
{
static void Main( string[] args )
{
Account[] accounts = {
new Account(),
new Account(),
new Account()
};
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( "\tUserID = , Username = ", 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
public string Username
}
class Program
{
static void Main( string[] args )
{
Account[] accounts = {
new Account(),
new Account(),
new Account()
};
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( "\tUserID = , Username = ", 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
public string Username
}
class Program
{
static void Main( string[] args )
{
Account[] accounts = {
new Account(),
new Account(),
new Account()
};
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( "\tUserID = , Username = ", i, deSerializedValue[ i ].UserID, deSerializedValue[ i ].Username );
}
}
}
}
Console.ReadKey();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询