2009年9月16日 星期三

SOAP序列化(M04)

SOAP序列化
SOAP(Simple Object Access Protocol)標準定義基本物件型別的XML描述方法,也定義了遠端程序呼叫的標準。
SOAP序列化和反序列化物件
SOAP結合XML序列化和Binary序列化的兩個重要特性,一是格式為標準XML,具備跨網際網路的特性,二是對於Private資料也能夠保存。
=====================
1、參考System.Runtime.Serialization.formatters.Soap.dll
2、SOAP序列化和反序列化物件
  • System.Runtime.Serialization.Formatters.Soap
  • 使用Formatter物件
  • 使用Stream物件
  • 呼叫Serialize和Deserialize方法
========================================
Public Class Person
Public strName As String = "Jerry"
Private intAge As Integer = 18
End Class
=====================

Imports System.Runtime.Serialization.Formatters.Soap
Imports System.IO
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 Person


'2、序列化SOAP物件
Dim Formatter As New SoapFormatter()
Dim fs As New FileStream(Application.StartupPath & "\Person.xml", FileMode.Create)

Formatter.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 Formatter As New SoapFormatter()
Dim fs As New FileStream(Application.StartupPath & "\Person.xml", FileMode.Open)
Dim obj As Person = CType(Formatter.Deserialize(fs), Person)
fs.Close()
MessageBox.Show("Deseriablize OK...Name = " & obj.strName)

End Sub
End Class

沒有留言:

張貼留言