VB.Net by API/System.Runtime.Serialization.Formatters.Soap/SoapFormatter — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:50, 26 мая 2010
New SoapFormatter
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Data.OleDb
Imports System.Data.rumon
Imports System.Data.SqlClient
Imports System.Data
Imports System.Runtime.Serialization.Formatters
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Xml.Serialization
Public Class MainClass
Shared Sub Main()
Dim Connection As SqlConnection = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
Dim MyDA As SqlDataAdapter = New SqlDataAdapter( _
"SELECT * FROM Employee", Connection)
Dim MyDS As DataSet = New DataSet()
Dim MyDS2 As DataSet = New DataSet()
MyDA.Fill(MyDS, "Employee")
Dim s As Stream = File.Open("MyDS.soap", _
FileMode.Create, FileAccess.ReadWrite)
Dim sf As SoapFormatter = New SoapFormatter()
sf.Serialize(s, MyDS)
s.Close()
Console.WriteLine("De-Serializing Graph from SOAP Envelope...")
Dim r As Stream = File.Open("MyDS.soap", _
FileMode.Open, FileAccess.Read)
Dim sf2 As SoapFormatter = New SoapFormatter()
MyDS2 = CType(sf2.Deserialize(r), DataSet)
r.Close()
Console.WriteLine( _
"After Deserialization, MyDS2 contains {0} Employee", _
MyDS2.Tables("Employee").Rows.Count)
End Sub
End Class
SoapFormatter.Deserialize
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap
Public Class Tester
Public Shared Sub Main
Dim myClsSerializable As New ClsSerializable()
SerializeSoap(myClsSerializable)
Console.WriteLine(FileContent(False))
Dim myFileStream As FileStream
myFileStream = New FileStream("test.dat", FileMode.Open, FileAccess.Read)
Dim myFormatter As New SoapFormatter()
myClsSerializable = CType(myFormatter.Deserialize(myFileStream), ClsSerializable)
Console.WriteLine(myClsSerializable.strMessage)
End Sub
Private Shared Function FileContent(ByVal blnBinary As Boolean) As String
Dim strContent As String
Dim myStreamReader As StreamReader
Dim myFileStream As FileStream
Dim i As Integer
Try
myFileStream = New FileStream("test.dat", FileMode.Open, FileAccess.Read)
If blnBinary = True Then
For i = 1 To myFileStream.Length
strContent += myFileStream.ReadByte.ToString + " "
Next
Else
myStreamReader = New StreamReader(myFileStream)
strContent = myStreamReader.ReadToEnd
End If
myFileStream.Flush()
myFileStream.Close()
Return strContent
Catch ex As IOException
Console.WriteLine(ex.Message)
End Try
End Function
Private Shared Sub SerializeSoap(ByVal myClsSerializable As ClsSerializable)
Dim myFileStream As FileStream
Dim myBFormatter As SoapFormatter = New SoapFormatter()
Try
myFileStream = New FileStream("test.dat", FileMode.Create, FileAccess.Write)
myBFormatter.Serialize(myFileStream, myClsSerializable)
myFileStream.Flush()
myFileStream.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Class
<Serializable()> _
Public Class ClsSerializable
Implements ISerializable
Public intNumber As Integer = 1000
Public strMessage As String = "this is a string"
Public lngTest As Long
Public intArrayX(10) As Integer
Public intArrayY(10) As Integer
Public Sub New()
ChangeMmberValue()
End Sub
Public Sub GetObjectData _
(ByVal info As SerializationInfo, ByVal context As StreamingContext) _
Implements ISerializable.GetObjectData
info.AddValue("intNumber", intNumber)
info.AddValue("message", strMessage)
info.AddValue("intArrayX", intArrayX)
End Sub
Public Sub New _
(ByVal info As SerializationInfo, ByVal context As StreamingContext)
intNumber = CInt(info.GetValue("intNumber", intNumber.GetType))
strMessage = CStr(info.GetValue("message", strMessage.GetType))
intArrayX = info.GetValue("intArrayX", intArrayX.GetType)
End Sub
Public Sub ChangeMmberValue()
Dim i As Integer
For i = 0 To 9
intArrayX(i) = i * 100
Next i
intNumber = 2000
strMessage = "this is another string"
End Sub
End Class
SoapFormatter.Serialize
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap
Public Class Tester
Public Shared Sub Main
Dim myClsSerializable As New ClsSerializable()
SerializeSoap(myClsSerializable)
Console.WriteLine(FileContent(False))
Dim myFileStream As FileStream
myFileStream = New FileStream("test.dat", FileMode.Open, FileAccess.Read)
Dim myFormatter As New SoapFormatter()
myClsSerializable = CType(myFormatter.Deserialize(myFileStream), ClsSerializable)
Console.WriteLine(myClsSerializable.intNumber.ToString)
Console.WriteLine(myClsSerializable.lngNumber.ToString)
Console.WriteLine(myClsSerializable.strDemo)
End Sub
Private Shared Function FileContent(ByVal blnBinary As Boolean) As String
Dim strContent As String
Dim myStreamReader As StreamReader
Dim myFileStream As FileStream
Dim i As Integer
Try
myFileStream = New FileStream("test.dat", FileMode.Open, FileAccess.Read)
If blnBinary = True Then
For i = 1 To myFileStream.Length
strContent += myFileStream.ReadByte.ToString + " "
Next
Else
myStreamReader = New StreamReader(myFileStream)
strContent = myStreamReader.ReadToEnd
End If
myFileStream.Flush()
myFileStream.Close()
Return strContent
Catch ex As IOException
Console.WriteLine(ex.Message)
End Try
End Function
Private Shared Sub SerializeSoap(ByVal myClsSerializable As ClsSerializable)
Dim myFileStream As FileStream
Dim myBFormatter As SoapFormatter = New SoapFormatter()
Try
myFileStream = New FileStream("test.dat", FileMode.Create, FileAccess.Write)
myBFormatter.Serialize(myFileStream, myClsSerializable)
myFileStream.Flush()
myFileStream.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Class
<Serializable()> Public Class ClsSerializable
Public intNumber As Integer = 254
Public strDemo As String = "This is a poublic test string"
Private strpDemo As String = "This is a private test string"
<NonSerialized()> Public lngNumber As Long = 123456
End Class