Поделиться через


System.Runtime.Serialization Namespace

Microsoft Silverlight will reach end of support after October 2021. Learn more.

The System.Runtime.Serialization namespace contains classes that can be used for serializing and deserializing objects.

Serialization is the process of converting an object or a graph of objects into a linear sequence of bytes for either storage or transmission to another location. Deserialization is the process of taking in stored information and re-creating objects from it.

Data Contract Classes

Two notable classes of this namespace are the DataContractAttribute and DataMemberAttribute attribute classes. Apply these attributes (to classes and class members, respectively) when creating Silverlight 5 clients to specify which members of a class contain data that must be serialized. You can also apply the EnumMemberAttribute to enumeration members to control the XML generated. And apply the CollectionDataContractAttribute to collection classes to control the XML elements generated from collections.

Data Contract Serialization

Another major class is the DataContractSerializer. Use this class to serialize or deserialize instances of classes that have the DataContractAttribute and DataMemberAttribute applied.

To create code from XSD schema documents, use the XsdDataContractImporter. With this class, you can read an XSD document, and generate System.CodeDom components that can be used to generate Visual Basic or Visual C# source code tailored to handling XML data.

Serialization without an Explicit Data Contract

Silverlight 5 also supports a simplified opt-out model for serialization, where these attributes can be omitted from the type and the members of the type to be serialized. The data contract is implicit in the sense that the visibility modifiers (public, private) used by the type determines whether it and its members are included in the data contract. In this model, the name of a member is used to identify it in the serialized representation. A new IgnoreDataMemberAttribute attribute can be used to opt-out a member that is public when required.

For more information about

serialization with an explicit data contract, see Silverlight Serialization.

Classes

  Class Description
Public class CollectionDataContractAttribute When applied to a collection type, enables custom specification of the collection item elements. This attribute can be applied only to types that are recognized by the DataContractSerializer as valid, serializable collections.
Public class ContractNamespaceAttribute Specifies the CLR namespace and XML namespace of the data contract.
Public class DataContractAttribute Specifies that the type defines or implements a data contract and can be serialized by a serializer, such as the DataContractSerializer.
Public class DataContractSerializer Serializes and deserializes an instance of a type into an XML stream or document using a supplied data contract. This class cannot be inherited.
Public class DataMemberAttribute When applied to the member of a type, specifies that the member is part of a data contract and is serializable by the DataContractSerializer.
Public class EnumMemberAttribute Specifies that the field is an enumeration member and should be serialized.
Public class IgnoreDataMemberAttribute When applied to the member of a type, specifies that the member is not part of a data contract and is not serialized.
Public class InvalidDataContractException The exception that is thrown when the DataContractSerializer encounters an invalid data contract during serialization and deserialization.
Public class KnownTypeAttribute Specifies types that should be recognized by the DataContractSerializer when serializing or deserializing a given type.
Public class OnDeserializedAttribute When applied to a method, specifies that the method is called immediately after deserialization of the object.
Public class OnDeserializingAttribute When applied to a method, specifies that the method is called during deserialization of an object.
Public class OnSerializedAttribute When applied to a method, specifies that the method is called after serialization of an object graph.
Public class OnSerializingAttribute When applied to a method, specifies that the method is called before serialization of an object.
Public class SerializationException The exception thrown when an error occurs during serialization or deserialization.
Public class XmlObjectSerializer Provides the base class used to serialize objects as XML streams or documents. This class is abstract.

Structures

  Structure Description
Public structure StreamingContext Describes the source and destination of a given serialized stream, and provides an additional caller-defined context.

Enumerations

  Enumeration Description
Public enumeration StreamingContextStates Defines a set of flags that specifies the source or destination context for the stream during serialization.