Confusion around SOAPFormatter vs. XMLSerializer


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Confusion around SOAPFormatter vs. XMLSerializer

  1. #1
    Aaron Anderson Guest

    Confusion around SOAPFormatter vs. XMLSerializer

    If I use SOAPFormatter when serializing my objects, all internal object data
    gets serialized, but in a format I don't like and can't control. If I use
    XMLSerializer to serialize my objects, I can control the format, but only
    public properties are being written, ignoring other internal object data.

    What am I doing wrong and how do I get out of this mess? Here's the sample
    VB.NET code:

    Imports System.IO
    Imports System.Xml.Serialization

    Module Module1

    Sub Main()

    'Create contact object and populate it with dummy data.
    Dim myContact As New Contact(123)
    myContact.FirstName = "Joe"
    myContact.LastName = "Smo"
    myContact.Address.AddressLine1 = "123 Main St."
    myContact.Address.AddressLine2 = "Somewhere, TX 78745"

    'Serialize using SOAPFormatter
    'This serializes all internal data, but in a format I don't want.
    Dim file As New FileStream("myContact.XML", FileMode.Create)
    Dim formatter As New
    System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
    formatter.Serialize(file, myContact)
    file.Close()

    'Serialize using XMLSerializer
    'This gets me the format, but not the internal data.
    Dim writer As New StreamWriter("myContact2.xml")
    Dim serializer As New
    System.Xml.Serialization.XmlSerializer(GetType(Contact))
    serializer.Serialize(writer, myContact)

    End Sub

    End Module

    Public Class <Serializable()> Contact


    Private myFirstName As String
    Private myLastName As String
    Private myContactID As Integer
    Private myAddress As Address

    Public Overloads Sub new(ByVal contactID As Integer)
    MyBase.New()
    myContactID = contactID
    myAddress = New Address()
    End Sub
    Public Overloads Sub new()
    Call Me.New(0)
    End Sub

    Public Property <XMLAttributeAttribute()> FirstName() As String
    Get
    Return myFirstName
    End Get
    Set
    myFirstName = value
    End Set
    End Property
    Public Property <XMLAttributeAttribute()> LastName() As String
    Get
    Return myLastName
    End Get
    Set
    myLastName = value
    End Set
    End Property
    Public Property <XMLElementAttribute()> Address() As Address
    Get
    Return myAddress
    End Get
    Set
    myAddress = value
    End Set
    End Property
    Public Overrides Function ToString() As String
    Return "myFirstName = " & myFirstName & ". myLastName = " &
    myLastName
    End Function
    End Class

    Public Class <Serializable()> Address
    Implements System.Runtime.Serialization.ISerializable

    Private myAddressLine1 As String
    Private myAddressLine2 As String

    Private Sub GetObjectData(ByVal info As
    System.Runtime.Serialization.SerializationInfo, ByVal context As
    System.Runtime.Serialization.StreamingContext) Implements
    System.Runtime.Serialization.ISerializable.GetObjectData
    Call info.AddValue("myAddressLine1", myAddressLine1)
    End Sub

    Public Property AddressLine1() As String
    Get
    Return myAddressLine1
    End Get
    Set
    myAddressLine1 = value
    End Set
    End Property

    Public Property AddressLine2() As String
    Get
    Return myAddressLine2
    End Get
    Set
    myAddressLine2 = value
    End Set
    End Property

    End Class




  2. #2
    Join Date
    Jun 2005
    Posts
    2
    Hi Aaron
    I'm new to all this too but have a stab at what I'm about to suggest and it could help you format your SOAP serializations. In the SoapFormatter class there's a property called SurrogateSelector that could help you. Have a look at the article "http://www.codeproject.com/dotnet/Surrogate_Serialization.asp" and let me know if it is what you are looking for.
    Thanks.
    Regards.
    Craig

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center