方法 : WCF アプリケーション内のメッセージをシリアル化します。
[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]
.NET Compact Framework バージョン 3. 5 Windows 通信基盤 (WCF) アプリケーションでのシリアル化および逆シリアル化メッセージのサポートを実装するために使用できる、XmlObjectSerializer クラス サポートを追加します。
使用例
クラス CFMessagingSerializerXmlObjectSerializer を実装するという名前し、メッセージを逆シリアル化およびシリアル化する両方のデバイスと、デスクトップに使用できますを次のコード例に示します。
Public
Class CFMessagingSerializer
Inherits XmlObjectSerializer
Private objectType As Type
Private serializer As XmlSerializer
PublicSubNew(ByVal objectType As Type)
Me.New(objectType, Nothing, Nothing)
EndSubPublicSubNew(ByVal objectType As Type, ByVal wrapperName AsString, ByVal wrapperNamespace AsString)
MyBase.New()
If (objectType IsNothing) ThenThrowNew ArgumentNullException("objectType")
EndIfIf ((wrapperName IsNothing) _
<> (wrapperNamespace = Nothing)) ThenThrowNew ArgumentException("wrapperName and wrapperNamespace must be either both null or both non-null.")
EndIfIf (wrapperName = String.Empty) ThenThrowNew ArgumentException("Cannot be the empty string.", "wrapperName")
EndIfMe.objectType = objectType
If (Not (wrapperName) IsNothing) ThenDim root As XmlRootAttribute = New XmlRootAttribute(wrapperName)
root.Namespace = wrapperNamespace
Me.serializer = New XmlSerializer(objectType, root)
ElseMe.serializer = New XmlSerializer(objectType)
EndIfEndSubPublicOverridesFunction IsStartObject(ByVal reader As XmlDictionaryReader) AsBooleanThrowNew NotImplementedException
EndFunctionPublicOverridesFunction ReadObject(ByVal reader As XmlDictionaryReader, ByVal verifyObjectName AsBoolean) AsObject
Debug.Assert((Not (serializer) IsNothing))
If (reader IsNothing) ThenThrowNew ArgumentNullException("reader")
EndIfIfNot verifyObjectName ThenThrowNew NotSupportedException
EndIfReturn serializer.Deserialize(reader)
EndFunctionPublicOverridesSub WriteStartObject(ByVal writer As XmlDictionaryWriter, ByVal graph AsObject)
ThrowNew NotImplementedException
EndSubPublicOverridesSub WriteObjectContent(ByVal writer As XmlDictionaryWriter, ByVal graph AsObject)
If (writer IsNothing) ThenThrowNew ArgumentNullException("writer")
EndIfIf (writer.WriteState <> WriteState.Element) ThenThrowNew SerializationException(String.Format("WriteState '{0}' not valid. Caller must write start element before serializing in contentOnly mode.", writer.WriteState))
EndIfDim 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 ThenWhile (reader.NodeType <> XmlNodeType.EndElement)
writer.WriteNode(reader, False)
EndWhile ' this will take us to the start of the next child node, or the end node.
reader.ReadEndElement()
' not necessary, but cleanEndIfEndSubPublicOverridesSub WriteEndObject(ByVal writer As XmlDictionaryWriter)
ThrowNew NotImplementedException
EndSubPublicOverridesSub WriteObject(ByVal writer As XmlDictionaryWriter, ByVal graph AsObject)
Debug.Assert((Not (serializer) IsNothing))
If (writer IsNothing) ThenThrowNew ArgumentNullException("writer")
EndIf
serializer.Serialize(writer, graph)
EndSubEndClass
public
class CFMessagingSerializer : XmlObjectSerializer
{
readonly Type objectType;
XmlSerializer serializer;
public CFMessagingSerializer(Type objectType)
: this(objectType, null, null)
{
}
public CFMessagingSerializer(Type objectType, string wrapperName, string wrapperNamespace)
{
if (objectType == null)
thrownew ArgumentNullException("objectType");
if ((wrapperName == null) != (wrapperNamespace == null))
thrownew ArgumentException("wrapperName and wrapperNamespace must be either both null or both non-null.");
if (wrapperName == string.Empty)
thrownew ArgumentException("Cannot be the empty string.", "wrapperName");
this.objectType = objectType;
if (wrapperName != null)
{
XmlRootAttribute root = new XmlRootAttribute(wrapperName);
root.Namespace = wrapperNamespace;
this.serializer = new XmlSerializer(objectType, root);
}
elsethis.serializer = new XmlSerializer(objectType);
}
publicoverridebool IsStartObject(XmlDictionaryReader reader)
{
thrownew NotImplementedException();
}
publicoverrideobject ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
{
Debug.Assert(serializer != null);
if (reader == null) thrownew ArgumentNullException("reader");
if (!verifyObjectName)
thrownew NotSupportedException();
return serializer.Deserialize(reader);
}
publicoverridevoid WriteStartObject(XmlDictionaryWriter writer, object graph)
{
thrownew NotImplementedException();
}
publicoverridevoid WriteObjectContent(XmlDictionaryWriter writer, object graph)
{
if (writer == null) thrownew ArgumentNullException("writer");
if (writer.WriteState != WriteState.Element)
thrownew SerializationException(string.Format("WriteState '{0}' not valid. Caller must write start element before serializing in contentOnly mode.",
writer.WriteState));
using (MemoryStream memoryStream = new MemoryStream())
{
using (XmlDictionaryWriter bufferWriter = XmlDictionaryWriter.CreateTextWriter(memoryStream, Encoding.UTF8))
{
serializer.Serialize(bufferWriter, graph);
bufferWriter.Flush();
memoryStream.Position = 0;
using (XmlReader reader = new XmlTextReader(memoryStream))
{
reader.MoveToContent();
writer.WriteAttributes(reader, false);
if (reader.Read()) // move off start node (we want to skip it)
{
while (reader.NodeType != XmlNodeType.EndElement) // also skip end node.
writer.WriteNode(reader, false); // this will take us to the start of the next child node, or the end node.
reader.ReadEndElement(); // not necessary, but clean
}
}
}
}
}
publicoverridevoid WriteEndObject(XmlDictionaryWriter writer)
{
thrownew NotImplementedException();
}
publicoverridevoid WriteObject(XmlDictionaryWriter writer, object graph)
{
Debug.Assert(serializer != null);
if (writer == null) thrownew ArgumentNullException("writer");
serializer.Serialize(writer, graph);
}
}
コードのコンパイル方法
この例では、次の名前空間への参照が必要です。