千家信息网

如何利用XMLSerializer将对象串行化到XML

发表于:2024-11-22 作者:千家信息网编辑
千家信息网最后更新 2024年11月22日,这篇文章主要为大家展示了"如何利用XMLSerializer将对象串行化到XML",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何利用XMLSeriali
千家信息网最后更新 2024年11月22日如何利用XMLSerializer将对象串行化到XML

这篇文章主要为大家展示了"如何利用XMLSerializer将对象串行化到XML",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何利用XMLSerializer将对象串行化到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# 代码说明了构造函数的用法。

    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文档:

Practical LotusScriptTonyPattonMale1 Main StreetAnywhereKY40000USA1884777767 43.95Manning Publications

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

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

Public Class AddressPublic Address1 As StringPublic Address2 As StringPublic City As StringPublic State As StringPublic Zip As StringPublic Country As StringEnd ClassPublic Class AuthorPublic FirstName As StringPublic MiddleName As StringPublic LastName As StringPublic Title As StringPublic Gender As StringPublic AddressObject As AddressEnd ClassPublic Class BookPublic AuthorObject As AuthorPublic Title As StringPublic ISBN As StringPublic RetailPrice As DoublePublic Publisher As StringEnd ClassImports System.Xml.SerializationImports System.IOModule Module1Sub Main()Dim BookObject As New BookDim 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 Withser.Serialize(writer, BookObject)writer.Close()End SubEnd Module

控制输出

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

Public Class BookPublic AuthorObject As AuthorPublic Title As String _Public ISBN As String _Public RetailPrice As DoublePublic Publisher As StringEnd Class

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

TonyPattonMale1 Main StreetAnywhereKY40000USAPractical LotusScriptManning Publications

以下是相应的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 BookDim 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));

以上是"如何利用XMLSerializer将对象串行化到XML"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0