微软已经意识到串行化数据的重要性,因此在.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# 代码说明了构造函数的用法。
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">
<Title>Practical LotusScript</Title>
<AuthorObject>
<FirstName>Tony</FirstName>
<LastName>Patton</LastName>
<Gender>Male</Gender>
<AddressObject>
<Address1>1 Main Street</Address1>
<City>Anywhere</City>
<State>KY</State>
<Zip>40000</Zip>
<Country>USA</Country>
</AddressObject>
</AuthorObject>
<ISBN>1884777767 </ISBN>
<RetailPrice>43.95</RetailPrice>
<Publisher>Manning 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>
<FirstName>Tony</FirstName>
<LastName>Patton</LastName>
<Gender>Male</Gender>
<AddressObject>
<Address1>1 Main Street</Address1>
<City>Anywhere</City>
<State>KY</State>
<Zip>40000</Zip>
<Country>USA</Country>
</AddressObject>
</AuthorObject>
<Title>Practical LotusScript</Title>
<Publisher>Manning Publications</Publisher>
</Book>
以下是相应的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认证。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 秀兰玛雅.1999-友情人【大旗】【WAV+CUE】
- 小米.2020-我想在城市里当一个乡下人【滚石】【FLAC分轨】
- 齐豫.2003-THE.UNHEARD.OF.CHYI.3CD【苏活音乐】【WAV+CUE】
- 黄乙玲1986-讲什么山盟海誓[日本东芝版][WAV+CUE]
- 曾庆瑜1991-柔情陷阱[台湾派森东芝版][WAV+CUE]
- 陈建江《享受男声》DTS-ES6.1【WAV】
- 群星《闪光的夏天 第5期》[FLAC/分轨][392.38MB]
- 徐小凤《三洋母带》1:1母盘直刻[WAV+CUE][981M]
- 王菲1995《菲靡靡之音》[香港首版][WAV+CUE][1G]
- 《双城之战》主题小游戏现已上线 扮演金克丝探索秘密基地
- 《霍格沃茨之遗》PS5Pro画面对比:光追性能显著提升
- 《怪猎荒野》PS5Pro主机版对比:B测性能都不稳定
- 黄宝欣.1992-黄宝欣金装精选2CD【HOMERUN】【WAV+CUE】
- 群星.1996-宝丽金流行爆弹精丫宝丽金】【WAV+CUE】
- 杜德伟.2005-独领风骚新歌精选辑3CD【滚石】【WAV+CUE】