Class SOAP Deserialization : SOAP Serialization « Socket Network « VB.Net Tutorial






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



254
This is a poublic  test string
This is a private  test string




254
0
This is a poublic  test string








22.32.SOAP Serialization
22.32.1.Class SOAP Serialization
22.32.2.Class SOAP Deserialization
22.32.3.SOAP serialization and deserialization for a Class with ISerializable implementation
22.32.4.SOAP serialization and deserialization for a derived Class