序列化与反序列化(XML、二进制)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//XML序列化
private void btnSerialization_Click(object sender, EventArgs e)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(BookStore));
string path = Application.StartupPath + @"\XmlBookStore.xml";
using (StreamWriter sw = new StreamWriter(path))
{
xmlSerializer.Serialize(sw, GenerateBookStore());
}
MessageBox.Show("OK");
}
//XML反序列化
private void btnDeSerialization_Click(object sender, EventArgs e)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(BookStore));
string path = Application.StartupPath + @"\XmlBookStore.xml";
FileStream fs = null;
try
{
fs = new FileStream(path,FileMode.Open, FileAccess.Read);
BookStore bookstore = (BookStore)xmlSerializer.Deserialize(fs);
if (bookstore.List_book.Count>0)
{
dataGridView1.DataSource = bookstore.List_book;
}
}
catch (Exception)
{
throw;
}
}
//二进制序列化
private void btnBinarrySerialization_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\BookStore.bin";
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
BinaryFormatter binaryformat = new BinaryFormatter();
binaryformat.Serialize(fs, GenerateBookStore());
}
MessageBox.Show("OK");
}
//二进制反序列化
private void btnBinarryDeserialization_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\BookStore.bin";
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
BinaryFormatter binaryformat = new BinaryFormatter();
BookStore bookstore = binaryformat.Deserialize(fs) as BookStore;
if (bookstore.List_book.Count > 0)
{
dataGridView1.DataSource = bookstore.List_book;
}
else
{
MessageBox.Show("无数据");
}
}
}
private BookStore GenerateBookStore()
{
Book book1 = new Book("地理", "2008-01", "美女");
Book book2 = new Book("历史", "2009-02", "帅哥");
BookStore bs = new BookStore();
bs.List_book.Add(book1);
bs.List_book.Add(book2);
return bs;
}
[Serializable]
public class Book
{
private string bookname;
private string publishdate;
private string author;
public Book()
{ }
public Book(string bookname, string publishdate, string author)
{
this.bookname = bookname;
this.publishdate = publishdate;
this.author = author;
}
public string Author
{
get { return author; }
set { author = value; }
}
public string Bookname
{
get { return bookname; }
set { bookname = value; }
}
public string Publishdate
{
get { return publishdate; }
set { publishdate = value; }
}
}
[Serializable]
public class BookStore
{
private List<Book> list_book = new List<Book>();
public BookStore()
{
}
public List<Book> List_book
{
get { return list_book; }
set { list_book = value; }
}
}
}
private void btnSerialization_Click(object sender, EventArgs e)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(BookStore));
string path = Application.StartupPath + @"\XmlBookStore.xml";
using (StreamWriter sw = new StreamWriter(path))
{
xmlSerializer.Serialize(sw, GenerateBookStore());
}
MessageBox.Show("OK");
}
//XML反序列化
private void btnDeSerialization_Click(object sender, EventArgs e)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(BookStore));
string path = Application.StartupPath + @"\XmlBookStore.xml";
FileStream fs = null;
try
{
fs = new FileStream(path,FileMode.Open, FileAccess.Read);
BookStore bookstore = (BookStore)xmlSerializer.Deserialize(fs);
if (bookstore.List_book.Count>0)
{
dataGridView1.DataSource = bookstore.List_book;
}
}
catch (Exception)
{
throw;
}
}
//二进制序列化
private void btnBinarrySerialization_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\BookStore.bin";
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
BinaryFormatter binaryformat = new BinaryFormatter();
binaryformat.Serialize(fs, GenerateBookStore());
}
MessageBox.Show("OK");
}
//二进制反序列化
private void btnBinarryDeserialization_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\BookStore.bin";
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
BinaryFormatter binaryformat = new BinaryFormatter();
BookStore bookstore = binaryformat.Deserialize(fs) as BookStore;
if (bookstore.List_book.Count > 0)
{
dataGridView1.DataSource = bookstore.List_book;
}
else
{
MessageBox.Show("无数据");
}
}
}
private BookStore GenerateBookStore()
{
Book book1 = new Book("地理", "2008-01", "美女");
Book book2 = new Book("历史", "2009-02", "帅哥");
BookStore bs = new BookStore();
bs.List_book.Add(book1);
bs.List_book.Add(book2);
return bs;
}
[Serializable]
public class Book
{
private string bookname;
private string publishdate;
private string author;
public Book()
{ }
public Book(string bookname, string publishdate, string author)
{
this.bookname = bookname;
this.publishdate = publishdate;
this.author = author;
}
public string Author
{
get { return author; }
set { author = value; }
}
public string Bookname
{
get { return bookname; }
set { bookname = value; }
}
public string Publishdate
{
get { return publishdate; }
set { publishdate = value; }
}
}
[Serializable]
public class BookStore
{
private List<Book> list_book = new List<Book>();
public BookStore()
{
}
public List<Book> List_book
{
get { return list_book; }
set { list_book = value; }
}
}
}