C#序列化与反序列化总结

界面效果图


总结

    /*XMLSerializer  XML序列化器*/
    /// <summary>
        /// 1.xml序列化必须添加 using System.Xml.Serialization
        /// 2.xml序列化可以序列化类型中的属性、公开字段,不能序列化私有字段
        /// 3.xml序列化可以在数据类型中使用[XmlIgnore]特性来标识该成员不被序列化
        /// </summary>

    /*BinarySerializer 二进制序列化器*/
    /// <summary>
        /// 1.二进制序列化器需要添加引用:System.Runtime.Serialization;
    /// 2.添加:using System.Runtime.Serialization.Formatters.Binary;
        /// 3.要在被序列化的类型上添加[Serializable]特性
        /// 4.在实例化序列化器的时候,不需要指定数据的类型type
        /// 5.二进制序列化器既可以序列化公有成员,也可以序列化私有成员
        /// 6.如果数据类型中,有个别字段不需要序列化,要使用[NonSerializable]特性修饰
        /// </summary>

    /*SoapSerializer  Soap序列化器*/
    /// <summary>
        /// 1.添加引用 System.Runtime.Serialization.Formatters.Soap;
        /// 2.添加 using System.Runtime.Serialization.Formatters.Soap;
        /// 3.soap 序列化器不能使用泛型集合当作要序列化的数据
        /// 4.soap 序列化器既可以序列化公有元素,也会序列化私有元素
        /// 5.soap 序列化器在序列化时,数据必须添加[Serialized]特性
        /// 6.如果不想序列化某个元素,可以加[NonSerialized]标签标注
        /// </summary>

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace SerializeTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 1.xml序列化必须添加 using System.Xml.Serialization
        /// 2.xml序列化可以序列化类型中的属性、公开字段,不能序列化私有字段
        /// 3.xml序列化可以在数据类型中使用[XmlIgnore]特性来标识该成员不被序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnXML_Click(object sender, EventArgs e)
        {

            List<StudentInfo> list = AddInfo();

            string path = @"F:\Asp.Net\2016-4-8\2016-4-8-09\XMLSerialize.xml";
            using (Stream fs = new FileStream(path, FileMode.OpenOrCreate))
            {
                XmlSerializer xml = new XmlSerializer(list.GetType());
                xml.Serialize(fs, list);
            }
            MessageBox.Show("序列化成功");
        }
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <returns></returns>
        private static List<StudentInfo> AddInfo()
        {
            List<StudentInfo> list = new List<StudentInfo>();
            for (int i = 0; i < 10; i++)
            {
                StudentInfo student = new StudentInfo();
                student.Name = "张三" + i.ToString();
                student.Number = i;
                student._age = 18 + i;
                list.Add(student);
            }
            return list;
        }
        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDXML_Click(object sender, EventArgs e)
        {
            string path = @"F:\Asp.Net\2016-4-8\2016-4-8-09\XMLSerialize.xml";
            using (Stream fs = new FileStream(path, FileMode.Open))
            {
                XmlSerializer xml = new XmlSerializer(typeof(List<StudentInfo>));
                object data = xml.Deserialize(fs);
                ListBoxShow(data);
            }
        }
        /// <summary>
        /// 反序列化显示数据
        /// </summary>
        /// <param name="data"></param>
        private void ListBoxShow(object data)
        {
            List<StudentInfo> list = (List<StudentInfo>)data;
            foreach (var item in list)
            {
                listBox1.Items.Add(" 姓名: " + item.Name + " 年龄: " + item._age );
            }
        }
        /// <summary>
        /// 1.二进制序列化器需要添加引用:System.Runtime.Serialization;还需要添加:using System.Runtime.Serialization.Formatters.Binary;
        /// 2.要在被序列化的类型上添加[Serializable]特性
        /// 3.在实例化序列化器的时候,不需要指定数据的类型type
        /// 4.二进制序列化器既可以序列化公有成员,也可以序列化私有成员
        /// 5.如果数据类型中,有个别字段不需要序列化,要使用[NonSerializable]特性修饰
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            List<StudentInfo> list = AddInfo();
            string path = @"F:\Asp.Net\2016-4-8\2016-4-8-09\BinarySerialize.xml";
            using (Stream fs = new FileStream(path,FileMode.OpenOrCreate))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs,list);
            }
            MessageBox.Show("序列化成功");
        }
        /// <summary>
        /// 二进制反序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"F:\Asp.Net\2016-4-8\2016-4-8-09\BinarySerialize.xml";
            using (Stream fs = new FileStream(path,FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
               var data = bf.Deserialize(fs);
               ListBoxShow(data);
            }
        }

        /// <summary>
        /// 1.添加引用 System.Runtime.Serialization.Formatters.Soap;
        /// 2.添加 using System.Runtime.Serialization.Formatters.Soap;
        /// 3.soap 序列化器不能使用泛型集合当作要序列化的数据
        /// 4.soap 序列化器既可以序列化公有元素,也会序列化私有元素
        /// 5.soap 序列化器在序列化时,数据必须添加[Serialized]特性
        /// 6.如果不想序列化某个元素,可以加[NonSerialized]标签标注
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            List<StudentInfo> list = AddInfo();
            string path = @"F:\Asp.Net\2016-4-8\2016-4-8-09\SoapSerialize.ini";
            using (Stream fs = new FileStream(path,FileMode.OpenOrCreate))
            {
            SoapFormatter sf = new SoapFormatter();
            sf.Serialize(fs,list.ToArray());
            }
            MessageBox.Show("序列化成功");
        }
        /// <summary>
        /// soap 反序列号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string path = @"F:\Asp.Net\2016-4-8\2016-4-8-09\SoapSerialize.ini";
            using (Stream fs = new FileStream(path,FileMode.Open))
            {
                SoapFormatter sf = new SoapFormatter();
                var data = sf.Deserialize(fs);
                listBox1.DataSource = data;
                listBox1.DisplayMember = "Name";
            }
        }
    }
}

using System;
using System.Xml.Serialization;

namespace SerializeTest
{
    [Serializable]
    public class StudentInfo
    {

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public int _age;
       // [NonSerialized]
        private string _sex = "女";
        [XmlIgnore]

        public int Number { get; set; }
    }
}

猜你喜欢

转载自blog.csdn.net/o527883184/article/details/51170428