使用SoapFormatter、XmlSerializer進行串行化

文章由通州北大青鳥校區(qū)學術部丁老師提供:

1.使用SoapFormatter進行串行化
通州北大青鳥校區(qū)丁老師介紹,與上一篇講解的BinaryFormatter類似,我們只需要做一下簡單修改即可:
a.將using語句中的.Formatter.Binary改為.Formatter.Soap;
b.將所有的BinaryFormatter替換為SoapFormatter.
c.確保報存文件的擴展名為.xml
經(jīng)過上面簡單改動,即可實現(xiàn)SoapFormatter的串行化,這時候產(chǎn)生的文件就是一個xml格式的文件。

2.使用XmlSerializer進行串行化
通州北大青鳥校區(qū)丁老師講解,關于格式化器,假設我們需要XML,但是不想要SOAP特有的額外信息,那么我們應該怎么辦呢?有兩中方案:編寫一個實現(xiàn)IFormatter接口的類,采用的方式類似于SoapFormatter類,但是沒有你不需要的信息;要么使用庫類XmlSerializer,這個類不使用Serializable屬性,但是它提供了類似的功能。

如果我們不想使用主流的串行化機制,而想使用XmlSeralizer進行串行化我們需要做一下修改:
a.添加System.Xml.Serialization命名空間。
b.Serializable和NoSerialized屬性將被忽略,而是使用XmlIgnore屬性,它的行為與NoSerialized類似。
c.XmlSeralizer要求類有個默認的構造器,這個條件可能已經(jīng)滿足了。

下面看示例:
要序列化的類:
    
 using System;
 using System.Data;
 using System.Configuration;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
 using System.Xml.Serialization;
 [Serializable]
 public class Person
  {
     private string name;
     public string Name
      {
         get
          {
             return name;
         }
         set
          {
             name = value;
         }
     }
 (北京北大青鳥校區(qū))
 
     public string Sex;
     public int Age = 31;
     public Course[] Courses;
 
     public Person()
      {
     }
     public Person(string Name)
      {
         name = Name;
         Sex = "男";
     }
 }
 [Serializable]
 public class Course
  {
     public string Name;
     [XmlIgnore]
     public string Description;
     public Course()
      {
     }
     public Course(string name, string description)
      {
         Name = name;
         Description = description;
     }
 } 
(北京北大青鳥校區(qū)

序列化和反序列化方法:
 public void XMLSerialize()
      {
         Person c = new Person("cyj");
         c.Courses = new Course[2];
         c.Courses[0] = new Course("英語", "交流工具");
         c.Courses[1] = new Course("數(shù)學","自然科學");
         XmlSerializer xs = new XmlSerializer(typeof(Person));
         Stream stream = new FileStream("c:\\cyj.XML",FileMode.Create,FileAccess.Write,FileShare.Read);
         xs.Serialize(stream,c);
         stream.Close();
     }
     public void XMLDeserialize()
      {
         XmlSerializer xs = new XmlSerializer(typeof(Person));
         Stream stream = new FileStream("C:\\cyj.XML",FileMode.Open,FileAccess.Read,FileShare.Read);
         Person p = xs.Deserialize(stream) as Person;
         Response.Write(p.Name);
         Response.Write(p.Age.ToString());
         Response.Write(p.Courses[0].Name);
         Response.Write(p.Courses[0].Description);
         Response.Write(p.Courses[1].Name);
         Response.Write(p.Courses[1].Description);
         stream.Close();
     }

北京北大青鳥校區(qū):這里Course類的Description屬性值將始終為null,生成的xml文檔中也沒有該節(jié)點,如下:
 <?xml version="1.0"?>
 <Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Sex>男</Sex>
   <Age>31</Age>
   <Courses>
     <Course>
       <Name>英語</Name>
       <Description>交流工具</Description>
     </Course>
     <Course>
       <Name>數(shù)學</Name>
       <Description>自然科學</Description>
     </Course>
   </Courses>
   <Name>cyj</Name>
 </Person>
北京北大青鳥校區(qū)提供,未完待續(xù)

北大青鳥網(wǎng)上報名
北大青鳥招生簡章