类型的序列化和反序列化

1、序列化含义
.Net程序执行时,对象都驻留在内存中;内存中的对象如果需要传递给其他系统使用;或者在关机时需要保存下来以便下次再次启动程序使用就需要序列化和反序列化。本文只介绍xml序列化,其实序列化可以是二进制的序列化,也可以是其他格式的序列化。
2、类型序列化
本程序介绍一个序列化和反序列化实例供参考。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;


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


        private void btnSave_Click(object sender, EventArgs e)
        {
            Book b1 = new Book("111", "Java");
            Book b2 = new Book("222", "C#");
            Book b3 = new Book("333", "Python");
            Books bs1 = new Books();
            Books bs2 = new Books();
            bs1.BookList.Add(b1);
            bs1.BookList.Add(b2);
            bs2.BookList.Add(b3);
            Person person = new Person("彬彬", 35);
            person.BookList.Add(bs1);
       
            //序列化
            using(FileStream fs=new FileStream("Output.xml",FileMode.OpenOrCreate))
            {
                XmlSerializer xs = new XmlSerializer(typeof(Person));
                xs.Serialize(fs, person);
            }

   //反序列化
            using(FileStream fs=new FileStream(System.AppDomain.CurrentDomain.BaseDirectory+"\\Output.xml",FileMode.Open))
            {
                XmlSerializer xs = new XmlSerializer(typeof(Person));
                Person p = (Person)xs.Deserialize(fs);
             
            }


        }
    }


    public class Person
    {
        string name;
        int age;
        List<Books> bookList = new List<Books>();


        /// <summary>
        /// 必须有默认的构造函数
        /// </summary>
        public Person()
        { }


        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }


        public string Name
        {
            get { return name; }
            set { name = value; }
        }


        public int Age
        {
            get { return age; }
            set { age = value; }
        }


        [XmlElement(ElementName = "Books")]
        public List<Books> BookList
        {
            get { return bookList; }
            set { bookList = value; }
        }
    }


    public class Books
    {
        List<Book> bookList = new List<Book>();


        [XmlElement(ElementName = "Book")]
        public List<Book> BookList
        {
            get { return bookList; }
            set { bookList = value; }
        }
    }


    public class Book
    {
        string isbn;
        string title;


        public Book() { }


        public Book(string isbn, string title)
        {
            this.isbn = isbn;
            this.title = title;
        }


        public string ISBN
        {
            get { return isbn; }
            set { isbn = value; }
        }


        public string Title
        {
            get { return title; }
            set { title = value; }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值