VB.Net Tutorial/Socket Network/SOAP Serialization

Материал из VB Эксперт
Перейти к: навигация, поиск

Class SOAP Deserialization

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
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.or
g/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://
schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.ru/soap/encoding/clr/1.0" S
OAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:ClsSerializable id="ref-1" xmlns:a1="http://schemas.microsoft.ru/clr/assem/main%2C%20Version%3D
0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<intNumber>254</intNumber>
<strDemo id="ref-3">This is a poublic  test string</strDemo>
<strpDemo id="ref-4">This is a private  test string</strpDemo>
</a1:ClsSerializable>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
254
0
This is a poublic  test string

Class SOAP Serialization

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))
    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
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.or
g/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://
schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.ru/soap/encoding/clr/1.0" S
OAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:ClsSerializable id="ref-1" xmlns:a1="http://schemas.microsoft.ru/clr/assem/main%2C%20Version%3D
0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<intNumber>254</intNumber>
<strDemo id="ref-3">This is a poublic  test string</strDemo>
<strpDemo id="ref-4">This is a private  test string</strpDemo>
</a1:ClsSerializable>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SOAP serialization and deserialization for a Class with ISerializable implementation

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
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.or
g/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://
schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.ru/soap/encoding/clr/1.0" S
OAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:ClsSerializable id="ref-1" xmlns:a1="http://schemas.microsoft.ru/clr/assem/main%2C%20Version%3D
0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<intNumber>2000</intNumber>
<message id="ref-3">this is another string</message>
<intArrayX href="#ref-4"/>
</a1:ClsSerializable>
<SOAP-ENC:Array id="ref-4" SOAP-ENC:arrayType="xsd:int[11]">
<item>0</item>
<item>100</item>
<item>200</item>
<item>300</item>
<item>400</item>
<item>500</item>
<item>600</item>
<item>700</item>
<item>800</item>
<item>900</item>
<item>0</item>
</SOAP-ENC:Array>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
this is another string

SOAP serialization and deserialization for a derived Class

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 ClsInherits()
        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 ClsInherits
    Inherits ClsSerializable
    Public intInNumber As Integer
    Public strInMessage As String
    Public Sub New()
        intInNumber = 1024
    End Sub
    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
        intInNumber = CInt(info.GetValue("n", intInNumber.GetType))
    End Sub
    Public Overrides Sub GetObjectData _
     (ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.GetObjectData(info, context)
        info.AddValue("n", intInNumber)
    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 Overridable 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
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.or
g/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://
schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.ru/soap/encoding/clr/1.0" S
OAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:ClsInherits id="ref-1" xmlns:a1="http://schemas.microsoft.ru/clr/assem/main%2C%20Version%3D0.0.
0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<intNumber>2000</intNumber>
<message id="ref-3">this is another string</message>
<intArrayX href="#ref-4"/>
<n>1024</n>
</a1:ClsInherits>
<SOAP-ENC:Array id="ref-4" SOAP-ENC:arrayType="xsd:int[11]">
<item>0</item>
<item>100</item>
<item>200</item>
<item>300</item>
<item>400</item>
<item>500</item>
<item>600</item>
<item>700</item>
<item>800</item>
<item>900</item>
<item>0</item>
</SOAP-ENC:Array>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
this is another string