C#温故而知新学习系列之XML编程—XmlSerializer类把对象序列化为XML文档(五)

  前言
    在XMLSerializer类中,包含了把对象序列化为XML文档和把XML文档反序列化为对象的方法,在创建一个类时,只要在该类的属性上加上自定义属性,XMLSerializer就可以读取这些属性,就可以将它们映射成XML元素和属性了

  在XMLSerializer类中,把对象序列化为XML文档用Serialize()方法实现,反序列化通过Deserialize()方法实现

  要对一个类进行序列化处理,可以在它的前面加上[Serializable]标签,也就是给类添加一个串行化属性,另外还存在[NonSerializable]标签,它只适用于被[Serializable]标签定义的类中的字段,作用是指示该字段不用被序列化

  我举一个书城的例子,把对象序列化为书城的XML文档

  阅读目录

  一:实现步骤

  二:运行效果

  实例

  一:实现步骤 

  1:代码文件编写

  1.1 BookShop.cs

  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Xml;
  using System.Xml.Serialization;

  namespace MyXmlSerializer
  {
      //这个标签表示整个类都需要序列化
      [Serializable()]
      [XmlRoot]
      public class BookShop
      {
          private Book _book;
          [XmlElement]
          public Book Book
          {
              get { return _book; }
              set { _book = value; }
          }
      }

      public class Book
      {
          string _booknumber = "";
          [XmlAttribute]
          public string BookNumber
          {
              get { return _booknumber; }
              set { _booknumber = value; }
          }

          string _name = "";
          [XmlElement]
          public string Name
          {
              get { return _name; }
              set { _name = value; }
          }

          string _othorname = "";
          [XmlElement]
          public string OthorName
          {
              get { return _othorname; }
              set { _othorname = value; }
          }

          string _author = "";
          [XmlElement]
          public string Author
          {
              get { return _author; }
              set { _author = value; }
          }

          string _money = "";
          [XmlElement]
          public string Money
          {
              get { return _money; }
              set { _money = value; }
          }

          string _issu = "";
          [XmlElement]
          public string Issu
          {
              get { return _issu; }
              set { _issu = value; }
          }
      }
  }

  1.2 Form1.cs

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

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

          /// <summary>
          /// 把对象序列化为XML文档
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          private void btnSerialize_Click(object sender, EventArgs e)
          {
              BookShop bookshhop = new BookShop();
              bookshhop.Book = new Book();
              bookshhop.Book.BookNumber = "001";
              bookshhop.Book.Name = "《西游记》";
              bookshhop.Book.OthorName = "大唐西游传";
              bookshhop.Book.Author = "吴承恩";
              bookshhop.Book.Money = "320";
              bookshhop.Book.Issu = "清华大学出版社";
              XmlSerializer xmlserializer = new XmlSerializer(typeof(BookShop));
              TextWriter textwriter = new StreamWriter("BookShop.xml");
              xmlserializer.Serialize(textwriter, bookshhop);
              textwriter.Close();
          }

     /// <summary>
          /// 把对象集合序列化为XML文档
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          private void button1_Click(object sender, EventArgs e)
          {
              List<BookShop> bookshhop_list = new List<BookShop>();
              for(int i = 0; i < 3; i++)
              {
                  BookShop bookshhop = new BookShop();
                  bookshhop.Book = new Book();
                  //这里应该是从数据库取得的,实体的属性值是不一样的,为了简化,我直接赋值都一样了
                  bookshhop.Book.BookNumber = "001";
                  bookshhop.Book.Name = "《西游记》";
                  bookshhop.Book.OthorName = "大唐西游传";
                  bookshhop.Book.Author = "吴承恩";
                  bookshhop.Book.Money = "320";
                  bookshhop.Book.Issu = "清华大学出版社";
                  bookshhop_list.Add(bookshhop);
              }
              XmlSerializer xmlserializer = new XmlSerializer(typeof(List<BookShop>));
              TextWriter textwriter = new StreamWriter("BookShopList.xml");
              xmlserializer.Serialize(textwriter, bookshhop_list);
              textwriter.Close();
          }
      }
  }

  二:运行效果

  当我们单击“把对象序列化为XML文档”按钮后,就会生成一个BookShop.xml文件,我们打开这个文件,效果如下,这就是把对象序列化为XML文档,当我们单击“把对象集合序列化为XML文档”按钮,效果如下,就实现了把对象集合序列化为XML文档了。

  

  

  

  

 

posted @ 2011-12-26 13:46  David.Meng  阅读(481)  评论(0编辑  收藏  举报