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; }
}
}
}
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; }
}
}
}