秀纳

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
对象的序列化分类:C#和ASP.NET代码

.net中对象的序列化是指将对象的状态存储起来,先将对象的字段和属性以及类名转换为字节流,然后再把字节流写入数据流。通过对对象反序列化,得到原对象完全相同的副本。

对象的序列化主要的目的是将对象持久化,经过持久化的对象可以从一个地方传输到另一个地方。


在.net中, IFormatter接口提供了对象序列化的功能。他有两个公有的方法:

反序列化对象方法
Deserialize : Deserializes the data on the provided stream and reconstitutes the graph of objects。

序列化对象方法
Serialize:Serializes an object, or graph of objects with the given root to the provided stream。

我们可以将对象序列化成两种格式:

BinaryFormatter :将对象序列化为二进制格式
SoapFormatter:将对象序列化为Soap格式

代码:

//要进行序列化的类
using System;
using System.Collections.Generic;
using System.Text;

namespace SerializeDemos
{
    [Serializable]
    
public class Car
    
{
        
private string _Price;
        
private string _Owner;
        
public string Price
        
{
            
get return this._Price; }
            
set this._Price = value; }
        }

        
public string Owner
        
{
            
get return this._Owner; }
            
set this._Owner = value; }
        }

        
public Car(string o, string p)
        
{
            
this.Price = p;
            
this.Owner = o;
        }

    }

}


//序列化以及反序列化对象
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;


namespace SerializeDemos
{

    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }

        
private void Form1_Load(object sender, EventArgs e)
        
{

        }


        
private void button1_Click(object sender, EventArgs e)
{
            Car car 
= new Car(this.txtOwner.Text, this.txtPrice.Text);
            FileStream fileStream 
= new FileStream("SerializedCar.bin", FileMode.Create);
            
// 用二进制格式序列化
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(fileStream, car);
            fileStream.Close();
            MessageBox.Show(
"Successful");
        }


        
private void button2_Click(object sender, EventArgs e)
        
{
            Car car 
= new Car(this.txtOwner.Text, this.txtPrice.Text);
            FileStream fileStream 
= new FileStream("SerializedCar.Soap", FileMode.Create);
            
// 序列化为Soap
            SoapFormatter formatter = new SoapFormatter();
            formatter.Serialize(fileStream, car);
            fileStream.Close();
            MessageBox.Show(
"Successful");
        }


        
private void button3_Click(object sender, EventArgs e)
        
{
            Car car 
= new Car(this.txtOwner.Text, this.txtPrice.Text);
            FileStream fileStream 
= new FileStream("SerializedCar.xml", FileMode.Create);
            
// 序列化为xml
            SoapFormatter formatter = new SoapFormatter();
            formatter.Serialize(fileStream, car);
            fileStream.Close();
            MessageBox.Show(
"Successful");
        }


        
private void button4_Click(object sender, EventArgs e)
        
{  
            System.Runtime.Serialization.IFormatter formatter 
= new BinaryFormatter();

//二进制格式反序列化
            Stream stream = new FileStream("SerializedCar.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            Car mycar 
= (Car)formatter.Deserialize(stream);
            stream.Close();
            MessageBox.Show(
string.Format("Price of Car: {0},\n Owner:{1}", mycar.Price, mycar.Owner));
        }


        
private void button5_Click(object sender, EventArgs e)
        
{
            System.Runtime.Serialization.IFormatter formatter 
= new SoapFormatter();
            
//Soap格式反序列化
            Stream stream = new FileStream("SerializedCar.Soap", FileMode.Open, FileAccess.Read, FileShare.Read);
            Car mycar 
= (Car)formatter.Deserialize(stream);
            stream.Close();
            MessageBox.Show(
string.Format("Price of Car: {0},\n Owner:{1}", mycar.Price, mycar.Owner));
        }


        
private void button6_Click(object sender, EventArgs e)
        
{
            System.Runtime.Serialization.IFormatter formatter 
= new SoapFormatter();
            
//Xml格式反序列化
            Stream stream = new FileStream("SerializedCar.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
posted on 2007-04-14 18:06  秀纳  阅读(404)  评论(0)    收藏  举报