主頁 > 知識庫 > 利用XMLSerializer將對象串行化到XML

利用XMLSerializer將對象串行化到XML

熱門標(biāo)簽:隨州外呼調(diào)研系統(tǒng) 高德地圖地圖標(biāo)注服務(wù)中心 r語言數(shù)據(jù)可視化地圖標(biāo)注 微信地圖標(biāo)注合并了 東營電銷 本地電話機器人 400電話辦理包年 南寧網(wǎng)絡(luò)外呼系統(tǒng)運營商 如何修改多個百度地圖標(biāo)注

    微軟已經(jīng)意識到串行化數(shù)據(jù)的重要性,因此在.NET框架中包含了命名空間System.Runtime.Serialization和System.Xml.Serialization以提供串行化功能,為用戶自己編寫串行化方法提供了一個框架。System.Xml.Serialization命名空間提供了將一個對象串行化為XML格式的基本方法。下面我們來看看如何使用這種方法。

XML的魅力

    串行化XML是指為了方便存儲或傳輸,把一個對象的公共的域和屬性保存為一種串行格式(這里是XML格式)的過程。非串行化則是使用串行的狀態(tài)信息將對象從串行XML狀態(tài)還原成原始狀態(tài)的過程。因此,可以把串行化看作是將對象的狀態(tài)保存到流或緩沖區(qū)中的一種方法。

串行化的目的是數(shù)據(jù)存儲和數(shù)據(jù)轉(zhuǎn)換。數(shù)據(jù)存儲指的是在用戶會話時保存數(shù)據(jù)。當(dāng)應(yīng)用程序關(guān)閉時,數(shù)據(jù)被保存(串行化),而當(dāng)用戶回來時,數(shù)據(jù)又被重新加載(非串行化)。數(shù)據(jù)轉(zhuǎn)換指將數(shù)據(jù)變換成能被另一個系統(tǒng)識別的格式。使用串行化和XML,可以很方便的進行數(shù)據(jù)轉(zhuǎn)換。

    對象中的數(shù)據(jù)可以是類、方法、屬性、私有類型、數(shù)組,在System.Xml.XmlElement或System.Xml.XmlAttribute對象中,它甚至可以是內(nèi)嵌的XML。

    System.Xml.Serialization命名空間中的關(guān)鍵類是XmlSerializer。當(dāng)然在該命名空間中還包括有關(guān)XML其他方面以及SOAP相關(guān)的其他類,不過我們的重點是XmlSerializer類。

XmlSerializer
    XmlSerializer類提供了把對象串行化為XML文件及把XML文檔非串行化為對象的方法。它還能讓用戶指定對象如何轉(zhuǎn)化為XML。可以把將被串行化的對象的類型作為類構(gòu)造函數(shù)的參數(shù)。下面的C# 代碼說明了構(gòu)造函數(shù)的用法。

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

    下面是等價的VB.NET代碼:

    Dim ser As New XmlSerializer(GetType(objectToSerialize))

    實際的串行化過程在XmlSerializer類的Serialize方法中實現(xiàn)。該方法允許在串行化過程中調(diào)用TextWriter、Stream和XmlWriter對象。下面的例子代碼說明了如何調(diào)用該方法。在本例中一個對象被串行化保存到本地磁盤的一個文件當(dāng)中。例子中首先是類聲明,后面緊接著是串行化源代碼。

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();
} } }

上面的代碼把三個對象變?yōu)橐粋€對象,因此在串行化過程中產(chǎn)生一個XML文件。以下是例子程序產(chǎn)生的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>

注意串行化過程也能處理對象數(shù)據(jù)的嵌套。數(shù)據(jù)被轉(zhuǎn)換成可識別的格式,方便了數(shù)據(jù)重載(非串行化)以及向另一個系統(tǒng)的數(shù)據(jù)傳輸。在數(shù)據(jù)傳輸過程時,接收方系統(tǒng)需要知道XML文件的格式(如果預(yù)先不知道的話)。因此需要提供一個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

控制輸出

 
串行化過程生成標(biāo)準(zhǔn)的XML文件,數(shù)據(jù)成員轉(zhuǎn)換為XML元素。不過,并非所有的數(shù)據(jù)成員都變成元素,可以通過在類代碼中添加一些標(biāo)記來控制輸出的XML文件。這樣,數(shù)據(jù)成員可以變換為XML屬性而非元素,也可以簡單的被忽略掉。下面的例子是一段經(jīng)過修改后的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

這段代碼告訴系統(tǒng)在生成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>

以下是相應(yīng)的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;
}

上面僅是稍微提了兩種標(biāo)記符號。請查閱.NET文檔以獲得完整的標(biāo)記符號。

非串行化
非串行化數(shù)據(jù)通過調(diào)用XmlSerializer類的Deserialize方法可以方便地實現(xiàn)。以下的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)

該程序把結(jié)果數(shù)據(jù)放入內(nèi)存?zhèn)溆?。下面是等價的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是一名專業(yè)應(yīng)用程序開發(fā)人員,知識面廣泛,獲得了Java、 VB、 Lotus和XML認(rèn)證。

您可能感興趣的文章:
  • jQuery-serialize()輸出序列化form表單值的方法
  • 非常好用的兩個PHP函數(shù) serialize()和unserialize()
  • 與jquery serializeArray()一起使用的函數(shù),主要來方便提交表單
  • PHP中json_encode、json_decode與serialize、unserialize的性能測試分析
  • jQuery ajax serialize()方法的使用以及常見問題解決

標(biāo)簽:益陽 西雙版納 寧夏 果洛 拉薩 德州 黃石 宿遷

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用XMLSerializer將對象串行化到XML》,本文關(guān)鍵詞  利用,XMLSerializer,將,對象,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用XMLSerializer將對象串行化到XML》相關(guān)的同類信息!
  • 本頁收集關(guān)于利用XMLSerializer將對象串行化到XML的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章