如何:序列化 WCF 应用程序中的消息
This page is specific to:.NET Framework Version:3.5
.NET Framework 开发人员指南
如何:序列化 WCF 应用程序中的消息

更新:2007 年 11 月

.NET Compact Framework 3.5 版增加了对 XmlObjectSerializer 类的支持,此类可用于实现对 Windows Communication Foundation (WCF) 应用程序中的消息进行序列化和反序列化的支持。

示例

下面的代码示例演示一个名为 CFMessagingSerializer 的类,该类实现 XmlObjectSerializer,可用于在设备和桌面上序列化和反序列化消息。

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


编译代码

此示例需要引用下面的命名空间:

请参见

其他资源

© 2009 Microsoft Corporation 版权所有。   保留所有权利 | 商标 | 隐私权声明
Page view tracker
为轻量型库评级
x
依无脚本原则生成的轻量型库 (loband),添加了大家要求的功能:搜索框和默认代码语言选择。
您喜欢这个搜索框吗?
您喜欢标签式代码块吗?
此主题有用吗?
提供详细反馈。
谢谢
x
感谢您帮助改善 MSDN Online。
反馈意见
切换视图
经典视图
轻量型视图
无脚本视图
切换视图