本文會示範如何使用
System.XmlSerialization.XmlSerializer 類別,來序列化及還原序列化物件可延伸標記語言 (XML)。
需求
下列清單列出建議的硬體、 軟體、 網路基礎結構及您需要的服務套件:
- Microsoft Visual Studio.NET
本文假設您已熟悉下列主題:
- Microsoft Visual Basic.NET
- XML
XML 序列化的描述
序列化是處理程序所用您的物件狀態儲存中的資料流。序列化可以讓您保存物件的狀態,以便稍後擷取狀態。序列化也可讓您複製現有物件建立新的物件。
System.Xml.Serialization 命名空間包含可以讓您將物件序列化為 XML 的類別。
在 Visual Basic.NET 中建立主控台應用程式
本章節中您建立主控台應用程式的:
- 會 XML 來將物件還原序列化。
- 將 XML 序列化儲存到文字檔案。
- 讀取 XML 文字檔案中建立新的物件 (還原序列化)。
- 請依照下列步驟執行 Visual Basic.NET 中建立新的主控台應用程式:
- 啟動 Visual Studio.NET。
- 在 [檔案] 功能表上指向 [新增],然後按一下 [專案]。
- 在 [新增專案] 對話方塊按一下 [專案類型 下的 [Visual Basic 專案],然後再按一下 [範本] 下方的 [主控台應用程式。
- 請依照下列步驟在專案中加入新的類別:
- 按一下 [專案] 功能表 加入類別。
- 在 [加入新項目] 對話方塊在 [名稱] 文字方塊中輸入 clsProduct.vb],然後再按一下 [開啟舊檔]。
- 在 clsProduct.vb 程式碼視窗中的程式碼取代下列程式碼:
Public Class clsProduct
Private mstrName As String
Private mstrDescription As String
Private mintQty As Integer
Public Property Name() As String
Get
Name = mstrName
End Get
Set(ByVal Value As String)
mstrName = Value
End Set
End Property
Public Property Description() As String
Get
Description = mstrDescription
End Get
Set(ByVal Value As String)
mstrDescription = Value
End Set
End Property
Public Property Qty() As Integer
Get
Qty = mintQty
End Get
Set(ByVal Value As Integer)
mintQty = Value
End Set
End Property
End Class
這個程式碼使用三個屬性,建立 Product 類別: 名稱]、 [描述],] 和 [Qty (數量)。 - 如 Module1.vb,切換到 [程式碼] 視窗,然後將下列程式碼加入至程式碼視窗的頂端:
Imports System.IO
Imports System.Xml.Serialization
- 在 Sub Main] 程序中加入下列程式碼建立並填入 clsProduct 類別的執行個體:
'Set up product object.
Dim p As New clsProduct()
p.Name = "Widget"
p.Description = "Faster, better, cheaper"
p.Qty = 5
- 序列化為 XML 物件,並將物件的狀態儲存到文字檔,請使用 XmlSerializer 物件。若要執行此動作將下列程式碼 End Sub 陳述式之前立即新增 Sub Main 程序中:
'Serialize object to a text file.
Dim objStreamWriter As New StreamWriter("C:\Product.xml")
Dim x As New XmlSerializer(p.GetType)
x.Serialize(objStreamWriter, p)
objStreamWriter.Close()
- 在 [偵錯] 功能表上按一下 [開始] 執行應用程式]。
- 在 [記事本] 或在 Microsoft Internet Explorer 中,請開啟 Product.xml 檔案。這個檔案的內容應該會出現,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<clsProduct xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>Widget</Name>
<Description>Faster, better, cheaper</Description>
<Qty>5</Qty>
</clsProduct>
- Xml.Serialization 命名空間可讓您自訂 XmlSerializer 類別會產生的輸出。比方說 clsProduct] 類別中 [數量] 欄位被縮寫為數量XmlElementAttribute 屬性可用來將欄位變更數量,當您序列化類別。
切換至 clsProduct.vb,程式碼視窗,然後將下列程式碼加入至程式碼視窗的頂端:
Imports System.Xml.Serialization
- 找尋 Qty,屬性程序,然後插入下列程式碼之前 公用屬性 Qty() 為 Integer 陳述式:
<XmlElementAttribute(ElementName:="Quantity")> _
- 在 [偵錯] 功能表上按一下 [開始] 執行應用程式]。
- 在 「 記事本 」 或 Internet Explorer 中,請開啟 Product.xml 檔案。這個檔案的內容應該如下所示:
<?xml version="1.0" encoding="utf-8"?>
<clsProduct xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>Widget</Name>
<Description>Faster, better, cheaper</Description>
<Quantity>5</Quantity>
</clsProduct>
請注意 Qty 項目會變成 數量。 - 如 Module1.vb,切換到 [程式碼] 視窗,然後將下列程式碼加入至 End Sub 陳述式之前緊接 Sub Main 程序:
'Deserialize text file to a new object.
Dim objStreamReader As New StreamReader("C:\Product.xml")
Dim p2 As New clsProduct()
p2 = x.Deserialize(objStreamReader)
objStreamReader.Close()
'Display property values of the new product object.
Console.WriteLine(p2.Name)
Console.WriteLine(p2.Description)
Console.WriteLine(CStr(p2.Qty))
Console.ReadLine()
這個程式碼會還原序列化 Product.xml 文字檔案,以建立新的 clsProduct 物件名為 p2。在另外這段程式碼會在主控台視窗中顯示 p2 的屬性值。
請確認它可以運作
在 [
偵錯] 功能表上按一下 [
開始] 執行應用程式]。請注意 p2 的屬性值顯示在主控台視窗中:
Widget
Faster, better, cheaper
5
完成程式碼清單
'Module1.vb
Imports System.Xml.Serialization
Imports System.IO
Module Module1
Sub Main()
'Set up product object.
Dim p As New clsProduct()
p.Name = "Widget"
p.Description = "Faster, better, cheaper"
p.Qty = 5
'Serialize object to a text file.
Dim objStreamWriter As New StreamWriter("C:\Product.xml")
Dim x As New XmlSerializer(p.GetType)
x.Serialize(objStreamWriter, p)
objStreamWriter.Close()
'Deserialize text file to a new object.
Dim objStreamReader As New StreamReader("C:\Product.xml")
Dim p2 As New clsProduct()
p2 = x.Deserialize(objStreamReader)
objStreamReader.Close()
'Display property values of the new product object.
Console.WriteLine(p2.Name)
Console.WriteLine(p2.Description)
Console.WriteLine(CStr(p2.Qty))
Console.ReadLine()
End Sub
End Module
'clsProduct.vb
Imports System.Xml.Serialization
Public Class clsProduct
Private mstrName As String
Private mstrDescription As String
Private mintQty As Integer
Public Property Name() As String
Get
Name = mstrName
End Get
Set(ByVal Value As String)
mstrName = Value
End Set
End Property
Public Property Description() As String
Get
Description = mstrDescription
End Get
Set(ByVal Value As String)
mstrDescription = Value
End Set
End Property
<XmlElementAttribute(ElementName:="Quantity")> _
Public Property Qty() As Integer
Get
Qty = mintQty
End Get
Set(ByVal Value As Integer)
mintQty = Value
End Set
End Property
End Class
如需詳細資訊,請參閱以下列 Microsoft 網站: