SOAP serialization and deserialization for a Class with ISerializable implementation : SOAP Serialization « Socket Network « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Socket Network » SOAP Serialization 
22.32.3.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 BooleanAs String
        Dim strContent As String
        Dim myStreamReader As StreamReader
        Dim myFileStream As FileStream
        Dim As Integer
        Try
            myFileStream = New FileStream("test.dat", FileMode.Open, FileAccess.Read)

            If blnBinary = True Then
                For i = 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(10As Integer
    Public intArrayY(10As 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 As Integer
        For i = To 9
            intArrayX(i= i * 100
        Next i
        intNumber = 2000
        strMessage = "this is another string"
    End Sub
End Class



2000
this is another string



0
100
200
300
400
500
600
700
800
900
0




this is another 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.