EmployeeEntity employee
=
new
EmployeeEntity();
employee.EmployeeNumber = " 1234 " ;
employee.FirstName = " lincoln " ;
employee.LastName = " lee " ;
employee.InputDateTime = DateTime.Now;
EmployeeEntity employee1 = new EmployeeEntity();
employee1.EmployeeNumber = " 2222 " ;
employee1.FirstName = " isabel " ;
employee1.LastName = " lv " ;
employee1.InputDateTime = DateTime.Now;
employee1.Nickname = " darling " ;
// 创建 BinaryFormatter 对象,用于序列化 EmployeeEntity 类型的对象
BinaryFormatter formatter = new BinaryFormatter();
// 将 EmployeeEntity 对象序列化为名为“MyBinaryEmployeeEntity.dat”的二进制文件
FileStream stream = File.Create( " MyBinaryEmployee.dat " );
formatter.Serialize(stream, employee);
formatter.Serialize(stream, employee1);
stream.Close();
IFormatter formatter1 = new BinaryFormatter();
Stream stream1 = new FileStream( " MyBinaryEmployee.dat " , FileMode.Open, FileAccess.Read, FileShare.Read);
EmployeeEntity obj;
while (stream1.Position != stream1.Length)
obj = (EmployeeEntity)formatter1.Deserialize(stream1);
stream1.Close();
employee.EmployeeNumber = " 1234 " ;
employee.FirstName = " lincoln " ;
employee.LastName = " lee " ;
employee.InputDateTime = DateTime.Now;
EmployeeEntity employee1 = new EmployeeEntity();
employee1.EmployeeNumber = " 2222 " ;
employee1.FirstName = " isabel " ;
employee1.LastName = " lv " ;
employee1.InputDateTime = DateTime.Now;
employee1.Nickname = " darling " ;
// 创建 BinaryFormatter 对象,用于序列化 EmployeeEntity 类型的对象
BinaryFormatter formatter = new BinaryFormatter();
// 将 EmployeeEntity 对象序列化为名为“MyBinaryEmployeeEntity.dat”的二进制文件
FileStream stream = File.Create( " MyBinaryEmployee.dat " );
formatter.Serialize(stream, employee);
formatter.Serialize(stream, employee1);
stream.Close();
IFormatter formatter1 = new BinaryFormatter();
Stream stream1 = new FileStream( " MyBinaryEmployee.dat " , FileMode.Open, FileAccess.Read, FileShare.Read);
EmployeeEntity obj;
while (stream1.Position != stream1.Length)
obj = (EmployeeEntity)formatter1.Deserialize(stream1);
stream1.Close();
其中EmployeeEntity的定义如下
EmployeeEntity
[Serializable]
public class EmployeeEntity
{
Private Member#region Private Member
private string employeeNumber;
private string employmentStatus;
private string nickname;
private string lastName;
private string firstName;
private DateTime inputDateTime;
#endregion
Public Accessor#region Public Accessor
public string EmployeeNumber
{
get { return employeeNumber; }
set { employeeNumber = value; }
}
public string EmploymentStatus
{
get { return employmentStatus; }
set { employmentStatus = value; }
}
public string Nickname
{
get { return nickname; }
set { nickname = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public DateTime InputDateTime
{
get { return inputDateTime; }
set { inputDateTime = value; }
}
#endregion
Supplement methods#region Supplement methods
public string Fullname
{
get { return firstName + " " + lastName; }
}
#endregion
public EmployeeEntity()
{
//
// TODO: Add constructor logic here
//
}
}
[Serializable]
public class EmployeeEntity
{
Private Member#region Private Member
private string employeeNumber;
private string employmentStatus;
private string nickname;
private string lastName;
private string firstName;
private DateTime inputDateTime;
#endregion
Public Accessor#region Public Accessor
public string EmployeeNumber
{
get { return employeeNumber; }
set { employeeNumber = value; }
}
public string EmploymentStatus
{
get { return employmentStatus; }
set { employmentStatus = value; }
}
public string Nickname
{
get { return nickname; }
set { nickname = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public DateTime InputDateTime
{
get { return inputDateTime; }
set { inputDateTime = value; }
}
#endregion
Supplement methods#region Supplement methods
public string Fullname
{
get { return firstName + " " + lastName; }
}
#endregion
public EmployeeEntity()
{
//
// TODO: Add constructor logic here
//
}
}
如果需要Xml文件方面的序列化操作,请参考文献1
参考文献
1.http://www.cnblogs.com/rickie/archive/2004/12/13/76208.html
2.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconbasicserialization.asp