- 跨AppDomain時使用
序列化技術同時也包含反序列化方法,當物件資料序列化成資料串流(Stream)格式,藉由網路送到其他位置時,需使用反序列化的方法將之還原成物件方能繼續使用物件
.NET相關序列化技術
1、Binary Serialization
- 提供物件的完整型別保存,通常用鄉跨不同應用程式之間的物件交換ex:剪貼簿可將物件序列化後放進剪貼簿,就像應用程式關閉,也可從剪貼簿反序列化到另一個應用程式使用。
- 將物件序列化之後可以轉換到 資料串流、硬體或記憶體,透過網路就可和各應用程式交換資料。ex:Romoting技術就是依靠序列化技術完成遠端呼叫的資料傳遞。
(1)建立一個可序列化的類別
- 套用Serializable Attribute
- 套用NonSerialized Attribute
- System.Runtime.Serialization.Formatters.Binary
- 使用Formatter物件
- 使用Stream物件
- 呼叫Serialize與Deserialize方法
Imports System.Runtime.Serialization.Formatters.binary
Imports System.IO
Private Name As String = "Jerry"
Private Age As Integer = 10
Public Property strName() As String
Get
Return Name
End Get
Set(ByVal value As String)
Name = value
End Set
End Property
Public Property intAge() As Integer
Get
Return Age
End Get
Set(ByVal value As Integer)
Age = value
End Set
End Property
Public Property dateBirthday() As DateTime
Get
Return Birthday
End Get
Set(ByVal value As DateTime)
Birthday = value
End Set
End Property
End Class
Imports System.IO
Public Class Form1
'1、建立物件及使用物件
Dim People As New BinaryClass()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
People.strName = "Jerry"
People.intAge = 18
People.dateBirthday = "2009/09/15"
'2、將序列化成Binary檔案
Dim formatter As New BinaryFormatter()
Dim fs As New FileStream(Application.StartupPath & "People.bin", FileMode.Create)
formatter.Serialize(fs, People)
fs.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'3、反序列化為物件
Dim formatter As New BinaryFormatter
Dim fs As New FileStream(Application.StartupPath & "People.bin", FileMode.Open)
Dim obj As BinaryClass = CType(formatter.Deserialize(fs), BinaryClass)
fs.Close()
Messagebox.Show("Deserialize OK...Private strName" & obj.strName)
'MessageBox.Show(Application.StartupPath & "People.bin")
End Sub
End Class
2、XML Serialization
- 只保存物件當中Public的成員資料與成員屬性,不保證資料完整性,通常應用在單純的資料保存及傳遞,且由於序列化後資料格式為XML,為一個開放的標準,適合於現今的網路中。
- 但因XML格式完全開放自行定義,會導致不相容於所有應用程式的情況發生,而SOAP標準就成為應用程式之前XML資料格式的標準了,讓資料更具通透性。
(1)建立一個類別包含Public的資料
(2)XML序列化和反序列化物件
- System.Xml.Serializtion
- 使用Stream物件
- 呼叫Serialize與Deserialize方法
Public Class MyBook
Public bookID As Integer
Public bookName As String
End Class
=====================
Imports System.IO
Imports System.Xml.Serialization
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'1、建立及使用物件
Dim obj As New MyBook
obj.bookID = 10
obj.bookName = "琦真說童年"
'2、序列化XML物件
Dim ser As New XmlSerializer(GetType(MyBook))
Dim fs As New FileStream(Application.StartupPath & "\MyBook.xml", FileMode.Create)
ser.Serialize(fs, obj)
fs.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'3、反序列化還原物件
Dim ser As New XmlSerializer(GetType(MyBook))
Dim fs As New FileStream(Application.StartupPath & "\MyBook.xml", FileMode.Open)
Dim obj As MyBook = CType(ser.Deserialize(fs), MyBook)
fs.Close()
MessageBox.Show("Deseriablize OK...PublicBookName = " & obj.bookName)
End Sub
End Class
使用Attribute控制XML序列化
(1)建立一個類別包含Public資料
(2)XML序列化及反序列化物件
(3)如何使用Attribute控制XML序列化
- System.Xml.Serialization Namespace
- XmlRoot Attribute
- XmlElement Attribute
- XmlAttribute Attribute
- XmlArray Attribute
- XmlArrayItem Attribute
Imports System.Xml.Serialization
End Class
=====================
Imports System.IO
Imports System.Xml.Serialization
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'1、建立及使用物件
Dim obj As New MyProduct
obj.productID = 123
obj.productName = "可愛小寶具"
'2、序列化XML物件
Dim ser As New XmlSerializer(GetType(MyProduct))
Dim fs As New FileStream(Application.StartupPath & "\MyProduct.xml", FileMode.Create)
ser.Serialize(fs, obj)
fs.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'3、反序列化還原物件
Dim ser As New XmlSerializer(GetType(MyProduct))
dim fs as New FileStream(application.StartupPath & "\MyProduct.xml",FileMode.Open)
Dim obj As MyProduct = CType(ser.Deserialize(fs), MyProduct)
fs.Close()
MessageBox.Show("Deseriablize OK...PublicBookName = " & obj.productName)
End Sub
End Class
使用控制Array型別序列化
Imports System.Xml.Serialization
End Class
=====================
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'1、建立及使用物件
Dim obj As New Group
Dim emp1 As New Employee
Dim emp2 As New Employee
emp1.Name = "Jerry"
emp2.Name = "John"
obj.Employees(0) = emp1
obj.Employees(1) = emp2
'2、序列化XML物件
Dim ser As New XmlSerializer(GetType(Group))
Dim fs As New FileStream(Application.StartupPath & "\Group.xml", FileMode.Create)
ser.Serialize(fs, CType(obj, Group))
fs.Close()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'3、反序列化還原物件
Dim ser As New XmlSerializer(GetType(Group))
Dim fs As New FileStream(Application.StartupPath & "\Group.xml", FileMode.Open)
Dim obj As Group = CType(ser.Deserialize(fs), Group)
fs.Close()
MessageBox.Show("Deseriablize OK...Group = " & obj.Employees(0).Name)
End Sub
沒有留言:
張貼留言