Net中的序列化与反序列化学习

1.要序列化的类
    [Serializable]
    
public class MyObject
    {
        
public int n1 = 0;
        
public int n2 = 0;
        
public string str = null;
    }

2.三种序列化方法
    /// <summary>
    
/// Net 中的序列化与反序列化 
    
/// </summary>
    public partial class Form6 : Form
    {
        
public Form6()
        {
            InitializeComponent();
        }

        
#region 序列化为一个XMl文件
        
private void button1_Click(object sender, EventArgs e)
        {
            MyObject o 
= new MyObject();
            o.n1 
= 9900;
            o.n2 
= 20;
            o.str 
= "hello";

            XmlSerializer ser 
= new XmlSerializer(o.GetType());
            FileStream fs 
= new FileStream(@"users.xml", FileMode.Create);

            ser.Serialize(fs, o);
            fs.Close();
        }
        
private void button4_Click(object sender, EventArgs e)
        {
            XmlSerializer ser 
= new XmlSerializer(typeof(MyObject));
            FileStream fs 
= new FileStream(@"users.xml", FileMode.Open);
            MyObject my 
= (MyObject)ser.Deserialize(fs);
            fs.Close();

            MessageBox.Show(my.n1.ToString());
        }
        
#endregion

        
#region 序列化为一个SOAP文件
        
private void button2_Click(object sender, EventArgs e)
        {
            MyObject o 
= new MyObject();
            o.n1 
= 888;
            o.n2 
= 20;
            o.str 
= "hello";

            IFormatter fm 
= new SoapFormatter();
            Stream sm 
= new FileStream("MySoap.xml", FileMode.Create, FileAccess.Write, FileShare.None);
            fm.Serialize(sm, o);
            sm.Close();
        }
        
private void button5_Click(object sender, EventArgs e)
        {
            IFormatter fm 
= new SoapFormatter();
            Stream sm 
= new FileStream("MySoap.xml", FileMode.Open, FileAccess.Read, FileShare.Read);

            MyObject obj 
= (MyObject)fm.Deserialize(sm);
            sm.Close();

            MessageBox.Show(obj.n1.ToString());
        }
        
#endregion

        
#region 序列化为一个二进制文件
        
private void button3_Click(object sender, EventArgs e)
        {
            MyObject o 
= new MyObject();
            o.n1 
= 100;
            o.n2 
= 20;
            o.str 
= "hello";

            IFormatter formatter 
= new BinaryFormatter();
            Stream sm 
= new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);

            formatter.Serialize(sm, o);
            sm.Close();
        }
        
private void button6_Click(object sender, EventArgs e)
        {
            IFormatter formatter 
= new BinaryFormatter();
            Stream stream 
= new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);

            MyObject obj 
= (MyObject)formatter.Deserialize(stream);
            stream.Close();

            MessageBox.Show(obj.n1.ToString());
        }
        
#endregion
    }

3.实现自定义序列化
改造实体类
    [Serializable]
    
public class MyObject : ISerializable
    {
        
public int n1 = 0;
        
public int n2 = 0;
        
public string str = null;

        
public MyObject()
        {

        }

        
public MyObject(SerializationInfo info,StreamingContext ctxt)
        {
            n1 
= (int)info.GetValue("n_1"typeof(int));
            str 
= (String)info.GetValue("str_test"typeof(string));
        }

        
public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(
"n_1", n1);
            info.AddValue(
"str_test", str);
        }
    }
序列与反序列
        private void button7_Click(object sender, EventArgs e)
        {
            MyObject o 
= new MyObject();
            o.n1 
= 7777;
            o.n2 
= 8888;
            o.str 
= "hello 9999";

            Stream steam 
= File.Open("temp3.dat", FileMode.Create);
            BinaryFormatter bf 
= new BinaryFormatter();
            bf.Serialize(steam, o);
            steam.Close();
            o 
= null;
        }

        
private void button8_Click(object sender, EventArgs e)
        {
            Stream sm 
= File.Open("temp3.dat", FileMode.Open);
            BinaryFormatter bf 
= new BinaryFormatter();

            MyObject o 
= (MyObject)bf.Deserialize(sm);
            sm.Close();

            MessageBox.Show(o.n1 
+ "\n" + o.n2 + "\n" + o.str);
        }
posted @ 2009-09-02 14:20  ╰⑥月の雨╮  阅读(319)  评论(0编辑  收藏  举报