How to: Serialize Messages in WCF Applications

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The .NET Compact Framework version 3.5 adds support for the XmlObjectSerializer class, which can be used to implement support for serialization and deserialization of messages in Windows Communication Foundation (WCF) applications.

The following code example shows a class named CFMessagingSerializer, which implements XmlObjectSerializer and can be used on both devices and on the desktop to serialize and deserialize messages.


Public Class CFMessagingSerializer
    Inherits XmlObjectSerializer

    Private objectType As Type

    Private serializer As XmlSerializer

    Public Sub New(ByVal objectType As Type)
        Me.New(objectType, Nothing, Nothing)

    End Sub

    Public Sub New(ByVal objectType As Type, ByVal wrapperName As String, ByVal wrapperNamespace As String)
        MyBase.New()
        If (objectType Is Nothing) Then
            Throw New ArgumentNullException("objectType")
        End If
        If ((wrapperName Is Nothing) _
                    <> (wrapperNamespace = Nothing)) Then
            Throw New ArgumentException("wrapperName and wrapperNamespace must be either both null or both non-null.")
        End If
        If (wrapperName = String.Empty) Then
            Throw New ArgumentException("Cannot be the empty string.", "wrapperName")
        End If
        Me.objectType = objectType
        If (Not (wrapperName) Is Nothing) Then
            Dim root As XmlRootAttribute = New XmlRootAttribute(wrapperName)
            root.Namespace = wrapperNamespace
            Me.serializer = New XmlSerializer(objectType, root)
        Else
            Me.serializer = New XmlSerializer(objectType)
        End If
    End Sub

    Public Overrides Function IsStartObject(ByVal reader As XmlDictionaryReader) As Boolean
        Throw New NotImplementedException
    End Function

    Public Overrides Function ReadObject(ByVal reader As XmlDictionaryReader, ByVal verifyObjectName As Boolean) As Object
        Debug.Assert((Not (serializer) Is Nothing))
        If (reader Is Nothing) Then
            Throw New ArgumentNullException("reader")
        End If
        If Not verifyObjectName Then
            Throw New NotSupportedException
        End If
        Return serializer.Deserialize(reader)
    End Function

    Public Overrides Sub WriteStartObject(ByVal writer As XmlDictionaryWriter, ByVal graph As Object)
        Throw New NotImplementedException
    End Sub

    Public Overrides Sub WriteObjectContent(ByVal writer As XmlDictionaryWriter, ByVal graph As Object)
        If (writer Is Nothing) Then
            Throw New ArgumentNullException("writer")
        End If
        If (writer.WriteState <> WriteState.Element) Then
            Throw New SerializationException(String.Format("WriteState '{0}' not valid. Caller must write start element before serializing in contentOnly mode.", writer.WriteState))
        End If
        Dim memoryStream As MemoryStream = New MemoryStream
        Dim bufferWriter As XmlDictionaryWriter = XmlDictionaryWriter.CreateTextWriter(memoryStream, Encoding.UTF8)
        serializer.Serialize(bufferWriter, graph)
        bufferWriter.Flush()
        memoryStream.Position = 0
        Dim reader As XmlReader = New XmlTextReader(memoryStream)
        reader.MoveToContent()
        writer.WriteAttributes(reader, False)
        If reader.Read Then

            While (reader.NodeType <> XmlNodeType.EndElement)
                writer.WriteNode(reader, False)

            End While
            ' this will take us to the start of the next child node, or the end node.
            reader.ReadEndElement()
            ' not necessary, but clean
        End If
    End Sub

    Public Overrides Sub WriteEndObject(ByVal writer As XmlDictionaryWriter)
        Throw New NotImplementedException
    End Sub

    Public Overrides Sub WriteObject(ByVal writer As XmlDictionaryWriter, ByVal graph As Object)
        Debug.Assert((Not (serializer) Is Nothing))
        If (writer Is Nothing) Then
            Throw New ArgumentNullException("writer")
        End If
        serializer.Serialize(writer, graph)
    End Sub
End Class


Show: