利用XMLSerializer将对象串行化到XML

lfxstar

lfxstar

2016-02-19 21:43

下面是个利用XMLSerializer将对象串行化到XML教程,撑握了其技术要点,学起来就简单多了。赶紧跟着图老师小编一起来看看吧!

      微软已经意识到串行化数据的重要性,因此在.NET框架中包含了命名空间System.Runtime.Serialization和System.Xml.Serialization以提供串行化功能,为用户自己编写串行化方法提供了一个框架。System.Xml.Serialization命名空间提供了将一个对象串行化为XML格式的基本方法。下面我们来看看如何使用这种方法。

  XML的魅力
   
      串行化XML是指为了方便存储或传输,把一个对象的公共的域和属性保存为一种串行格式(这里是XML格式)的过程。非串行化则是使用串行的状态信息将对象从串行XML状态还原成原始状态的过程。因此,可以把串行化看作是将对象的状态保存到流或缓冲区中的一种方法。

  串行化的目的是数据存储和数据转换。数据存储指的是在用户会话时保存数据。当应用程序关闭时,数据被保存(串行化),而当用户回来时,数据又被重新加载(非串行化)。数据转换指将数据变换成能被另一个系统识别的格式。使用串行化和XML,可以很方便的进行数据转换。

      对象中的数据可以是类、方法、属性、私有类型、数组,在System.Xml.XmlElement或System.Xml.XmlAttribute对象中,它甚至可以是内嵌的XML。

      System.Xml.Serialization命名空间中的关键类是XmlSerializer。当然在该命名空间中还包括有关XML其他方面以及SOAP相关的其他类,不过我们的重点是XmlSerializer类。

  XmlSerializer
      XmlSerializer类提供了把对象串行化为XML文件及把XML文档非串行化为对象的方法。它还能让用户指定对象如何转化为XML。可以把将被串行化的对象的类型作为类构造函数的参数。下面的C# 代码说明了构造函数的用法。

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/)

      XmlSerializer ser = new XmlSerializer(typeof(objectToSerialize));

      下面是等价的VB.NET代码:

      Dim ser As New XmlSerializer(GetType(objectToSerialize))

      实际的串行化过程在XmlSerializer类的Serialize方法中实现。该方法允许在串行化过程中调用TextWriter、Stream和XmlWriter对象。下面的例子代码说明了如何调用该方法。在本例中一个对象被串行化保存到本地磁盘的一个文件当中。例子中首先是类声明,后面紧接着是串行化源代码。

  using System;
  namespace BuilderSerialization {
  public class Address {
  public Address() {}
  public string Address1;
  public string Address2;
  public string City;
  public string State;
  public string Zip;
  public string Country;
  } }
  using System;
  namespace BuilderSerialization {
  public class Author {
  public Author() { }
  public string FirstName;
  public string MiddleName;
  public string LastName;
  public string Title;
  public string Gender;
  public Address AddressObject;
  } }
  namespace BuilderSerialization {
  public class Book {
  public Book() { }
  public string Title;
  public Author AuthorObject;
  public string ISBN;
  public double RetailPrice;
  public string Publisher;
  }}
  using System;
  using System.Xml.Serialization;
  using System.IO;
  namespace BuilderSerialization {
  class TestClass {
  static void Main(string[] args) {
  Book BookObject = new Book();
  XmlSerializer ser = new XmlSerializer(typeof(Book));
  TextWriter writer = new StreamWriter("booktest.xml");
  BookObject.Title = "Practical LotusScript";
  BookObject.ISBN = "1884777767 ";
  BookObject.Publisher = "Manning Publications";
  BookObject.RetailPrice = 43.95;
  BookObject.AuthorObject = new Author();
  BookObject.AuthorObject.FirstName = "Tony";
  BookObject.AuthorObject.LastName = "Patton";
  BookObject.AuthorObject.Gender = "Male";
  BookObject.AuthorObject.AddressObject = new Address();
  BookObject.AuthorObject.AddressObject.Address1 = "1 Main Street";
  BookObject.AuthorObject.AddressObject.City = "Anywhere";
  BookObject.AuthorObject.AddressObject.State = "KY";
  BookObject.AuthorObject.AddressObject.Zip = "40000";
  BookObject.AuthorObject.AddressObject.Country = "USA";
  ser.Serialize(writer, BookObject);
  writer.Close();
  } } }

  上面的代码把三个对象变为一个对象,因此在串行化过程中产生一个XML文件。以下是例子程序产生的XML文档:

  ?xml version="1.0" encoding="utf-8"?
  Book xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  TitlePractical LotusScript/Title
  AuthorObject
  FirstNameTony/FirstName
  LastNamePatton/LastName
  GenderMale/Gender
  AddressObject
  Address11 Main Street/Address1
  CityAnywhere/City
  StateKY/State
  Zip40000/Zip
  CountryUSA/Country
  /AddressObject
  /AuthorObject
  ISBN1884777767 /ISBN
  RetailPrice43.95/RetailPrice
  PublisherManning Publications/Publisher
  /Book

  注意串行化过程也能处理对象数据的嵌套。数据被转换成可识别的格式,方便了数据重载(非串行化)以及向另一个系统的数据传输。在数据传输过程时,接收方系统需要知道XML文件的格式(如果预先不知道的话)。因此需要提供一个XML schema文件。.NET框架中的XSD.exe工具可以为串行化XML生成一个schema文件。

  下面是用VB.NET编写的例子代码:

  Public Class Address
  Public Address1 As String
  Public Address2 As String
  Public City As String
  Public State As String
  Public Zip As String
  Public Country As String
  End Class
  Public Class Author
  Public FirstName As String
  Public MiddleName As String
  Public LastName As String
  Public Title As String
  Public Gender As String
  Public AddressObject As Address
  End Class
  Public Class Book
  Public AuthorObject As Author
  Public Title As String
  Public ISBN As String
  Public RetailPrice As Double
  Public Publisher As String
  End Class
  Imports System.Xml.Serialization
  Imports System.IO
  Module Module1
  Sub Main()
  Dim BookObject As New Book
  Dim ser As New XmlSerializer(GetType(Book))
  Dim writer As New StreamWriter("booktest.xml")
  With BookObject
  .Title = "Practical LotusScript"
  .ISBN = "1884777767 "
  .Publisher = "Manning Publications"
  .RetailPrice = 43.95
  .AuthorObject = New Author
  .AuthorObject.FirstName = "Tony"
  .AuthorObject.LastName = "Patton"
  .AuthorObject.Gender = "Male"
  .AuthorObject.AddressObject = New Address
  .AuthorObject.AddressObject.Address1 = "1 Main Street"
  .AuthorObject.AddressObject.City = "Anywhere"
  .AuthorObject.AddressObject.State = "KY"
  .AuthorObject.AddressObject.Zip = "40000"
  .AuthorObject.AddressObject.Country = "USA"
  End With
  ser.Serialize(writer, BookObject)
  writer.Close()
  End Sub
  End Module

  控制输出
   
   
  串行化过程生成标准的XML文件,数据成员转换为XML元素。不过,并非所有的数据成员都变成元素,可以通过在类代码中添加一些标记来控制输出的XML文件。这样,数据成员可以变换为XML属性而非元素,也可以简单的被忽略掉。下面的例子是一段经过修改后的book类VB.NET代码。

  Public Class Book
  Public AuthorObject As Author
  Public Title As String
  System.Xml.Serialization.XmlAttribute() _
  Public ISBN As String
  System.Xml.Serialization.XmlIgnoreAttribute() _
  Public RetailPrice As Double
  Public Publisher As String
  End Class

  这段代码告诉系统在生成XML文件时把类成员ISBN作为XML属性,同时忽略掉RetailPrice成员。这种变化可以在生成的XML文件中可以看出:

  ?xml version="1.0" encoding="utf-8"?
  Book xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ISBN="1884777767 "
  AuthorObject
  FirstNameTony/FirstName
  LastNamePatton/LastName
  GenderMale/Gender
  AddressObject
  Address11 Main Street/Address1
  CityAnywhere/City
  StateKY/State
  Zip40000/Zip
  CountryUSA/Country
  /AddressObject
  /AuthorObject
  TitlePractical LotusScript/Title
  PublisherManning Publications/Publisher
  /Book

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/)

  以下是相应的C# 代码:

  public class Book {
  public Book() { }
  public string Title;
  public Author AuthorObject;
  [System.Xml.Serialization.XmlAttribute()]
  public string ISBN;
  [System.Xml.Serialization.XmlIgnoreAttribute()]
  public double RetailPrice;
  public string Publisher;
  }

  上面仅是稍微提了两种标记符号。请查阅.NET文档以获得完整的标记符号。

  非串行化
  非串行化数据通过调用XmlSerializer类的Deserialize方法可以方便地实现。以下的VB.NET程序片断完成上文中XML文档的非串行化:

  Dim BookObject As New Book
  Dim ser As New XmlSerializer(GetType(Book))
  Dim fs As New System.IO.FileStream("booktest.xml", FileMode.Open)
  Dim reader As New System.XML.XmlTextReader(fs)
  BookObject = CType(ser.Deserialize(reader), Book)

  该程序把结果数据放入内存备用。下面是等价的C# 代码:

  XmlSerializer ser = new XmlSerializer(typeof(Book));
  System.IO.FileStreamfs = new System.IO.FileStream("booktest.xml",
  FileMode.Open);
  System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(fs);
  Book BookObject = (Book)(ser.Deserialize(reader));

  
  ---------------------------------------------------
  作者简介:Tony Patton是一名专业应用程序开发人员,知识面广泛,获得了Java、 VB、 Lotus和XML认证。

展开更多 50%)
分享

猜你喜欢

利用XMLSerializer将对象串行化到XML

Web开发
利用XMLSerializer将对象串行化到XML

第十三节--对象串行化

Web开发
第十三节--对象串行化

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

串行化(Serialization)

C语言教程 C语言函数
串行化(Serialization)

第十三节 对象串行化 [13]

Web开发
第十三节 对象串行化 [13]

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

.NET使XML串行化易如反掌

Web开发
.NET使XML串行化易如反掌

PHP中的串行化变量和序列化对象

PHP
PHP中的串行化变量和序列化对象

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

学会这几招让XP系统轻装加速

学会这几招让XP系统轻装加速

将图片读入到Dom中并将其存为xml文件

将图片读入到Dom中并将其存为xml文件
下拉加载更多内容 ↓