Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

DataContractSerializer Class

Serializes and deserializes an instance of a type into an XML stream or document using a supplied data contract. This class cannot be inherited.

System.Object
  System.Runtime.Serialization.XmlObjectSerializer
    System.Runtime.Serialization.DataContractSerializer

Namespace:  System.Runtime.Serialization
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)
public sealed class DataContractSerializer : XmlObjectSerializer

The DataContractSerializer type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneDataContractSerializer(Type)Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type.
Public methodSupported by Silverlight for Windows PhoneDataContractSerializer(Type, IEnumerable<Type>)Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type, and a collection of known types that may be present in the object graph.
Public methodSupported by Silverlight for Windows PhoneDataContractSerializer(Type, String, String)Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type using the supplied XML root element and namespace.
Public methodSupported by Silverlight for Windows PhoneDataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type using the XML root element and namespace specified through the parameters of type XmlDictionaryString.
Public methodSupported by Silverlight for Windows PhoneDataContractSerializer(Type, String, String, IEnumerable<Type>)Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two string parameters as well as a list of known types that may be present in the object graph.
Public methodSupported by Silverlight for Windows PhoneDataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two XmlDictionaryString parameters as well as a list of known types that may be present in the object graph.
Top
  NameDescription
Public propertySupported by Silverlight for Windows PhoneIgnoreExtensionDataObjectGets a value that specifies whether to ignore data supplied by an extension of the class when the class is being serialized or deserialized.
Public propertySupported by Silverlight for Windows PhoneKnownTypesGets a collection of types that may be present in the object graph serialized using this instance of the DataContractSerializer.
Public propertySupported by Silverlight for Windows PhoneMaxItemsInObjectGraphGets the maximum number of items in an object graph to serialize or deserialize.
Public propertySupported by Silverlight for Windows PhonePreserveObjectReferencesGets a value that specifies whether to use non-standard XML constructs to preserve object reference data.
Top
  NameDescription
Public methodSupported by Silverlight for Windows PhoneEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneIsStartObject(XmlDictionaryReader)Determines whether the XmlDictionaryReader is positioned on an object that can be deserialized. (Overrides XmlObjectSerializer.IsStartObject(XmlDictionaryReader).)
Public methodSupported by Silverlight for Windows PhoneIsStartObject(XmlReader)Determines whether the XmlReader is positioned on an object that can be deserialized. (Overrides XmlObjectSerializer.IsStartObject(XmlReader).)
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneReadObject(Stream)Reads the XML stream or document with a Stream and returns the deserialized object. (Inherited from XmlObjectSerializer.)
Public methodSupported by Silverlight for Windows PhoneReadObject(XmlDictionaryReader)Reads the XML document or stream with an XmlDictionaryReader and returns the deserialized object. (Inherited from XmlObjectSerializer.)
Public methodSupported by Silverlight for Windows PhoneReadObject(XmlReader)Reads the XML stream with an XmlReader and returns the deserialized object. (Overrides XmlObjectSerializer.ReadObject(XmlReader).)
Public methodSupported by Silverlight for Windows PhoneReadObject(XmlDictionaryReader, Boolean)Reads the XML stream with an XmlDictionaryReader and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value. (Overrides XmlObjectSerializer.ReadObject(XmlDictionaryReader, Boolean).)
Public methodSupported by Silverlight for Windows PhoneReadObject(XmlReader, Boolean)Reads the XML stream with an XmlReader and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value. (Overrides XmlObjectSerializer.ReadObject(XmlReader, Boolean).)
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneWriteEndObject(XmlDictionaryWriter)Writes the closing XML element using an XmlDictionaryWriter. (Overrides XmlObjectSerializer.WriteEndObject(XmlDictionaryWriter).)
Public methodSupported by Silverlight for Windows PhoneWriteEndObject(XmlWriter)Writes the closing XML element using an XmlWriter. (Overrides XmlObjectSerializer.WriteEndObject(XmlWriter).)
Public methodSupported by Silverlight for Windows PhoneWriteObject(Stream, Object)Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified Stream. (Inherited from XmlObjectSerializer.)
Public methodSupported by Silverlight for Windows PhoneWriteObject(XmlDictionaryWriter, Object)Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified XmlDictionaryWriter. (Inherited from XmlObjectSerializer.)
Public methodSupported by Silverlight for Windows PhoneWriteObject(XmlWriter, Object)Writes all the object data (starting XML element, content, and closing element) to an XML document or stream with an XmlWriter. (Overrides XmlObjectSerializer.WriteObject(XmlWriter, Object).)
Public methodSupported by Silverlight for Windows PhoneWriteObjectContent(XmlDictionaryWriter, Object)Writes the XML content using an XmlDictionaryWriter. (Overrides XmlObjectSerializer.WriteObjectContent(XmlDictionaryWriter, Object).)
Public methodSupported by Silverlight for Windows PhoneWriteObjectContent(XmlWriter, Object)Writes the XML content using an XmlWriter. (Overrides XmlObjectSerializer.WriteObjectContent(XmlWriter, Object).)
Public methodSupported by Silverlight for Windows PhoneWriteStartObject(XmlDictionaryWriter, Object)Writes the opening XML element using an XmlDictionaryWriter. (Overrides XmlObjectSerializer.WriteStartObject(XmlDictionaryWriter, Object).)
Public methodSupported by Silverlight for Windows PhoneWriteStartObject(XmlWriter, Object)Writes the opening XML element using an XmlWriter. (Overrides XmlObjectSerializer.WriteStartObject(XmlWriter, Object).)
Top

Use the DataContractSerializer class to serialize and deserialize instances of a type into an XML stream or document. For example, you can create a type named Person with properties that contain essential data, such as a name and address. You can then create and manipulate an instance of the Person class and write all of its property values in an XML document for later retrieval, or in an XML stream for immediate transport. Most important, the DataContractSerializer is used to serialize and deserialize data sent in Silverlight 5 messages. Apply the DataContractAttribute attribute to classes, and the DataMemberAttribute attribute to class members to specify properties and fields that are serialized.

To use the DataContractSerializer, first create an instance of a class and an object appropriate to writing or reading the format; for example, an instance of the XmlDictionaryWriter. Then call the WriteObject method to persist the data. To retrieve data, create an object appropriate to reading the data format (such as an XmlDictionaryReader for an XML document) and call the ReadObject method.

Preparing Classes for Serialization or Deserialization

The DataContractSerializer is used in combination with the DataContractAttribute and DataMemberAttribute classes. To prepare a class for serialization, apply the DataContractAttribute to the class. For each member of the class that returns data that you want to serialize, apply the DataMemberAttribute. You can serialize fields and properties, provided they are publicly accessible.

For example, your schema specifies a Customer with an ID property, but you already have an existing application that uses a type named Person with a Name property. To create a type that conforms to the contract, first apply the DataContractAttribute to the class. Then apply the DataMemberAttribute to every field or property that you want to serialize.

NoteNote:

You can apply the DataMemberAttribute to both private and public members.

The final format of the XML need not be text. Instead, the DataContractSerializer writes the data as an XML infoset, which allows you to write the data to any format recognized by the XmlReader and XmlWriter. It is recommended that you use the XmlDictionaryReader and XmlDictionaryWriter classes to read and write, because both are optimized to work with the DataContractSerializer.


namespace SL_DataContractSerializer
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        //This uses an event handler, not Silverlight data binding
        void OnClick(object sender, EventArgs args)
        {
            txtOutput1.Text = "Create a User object and serialize it.";
            string serializedUser = WriteFromObject();
            txtOutput2.Text = serializedUser;

        }


        // Create a User object and serializing it.
        public static string WriteFromObject()
        {
            //Create User object.
            User user = new User("Bob", 42);

            //Create a stream to serialize the object to.
            MemoryStream ms = new MemoryStream();

            // Serializer the User object to the stream.
            DataContractSerializer ser = new DataContractSerializer(typeof(User));
            ser.WriteObject(ms, user);
            byte[] array = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(array, 0, array.Length);
        }

    }

    [DataContract]
    public class User
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public int Age { get; set; }

        public User() { }

        public User(string newName, int newAge)
        {
            Name = newName;
            Age = newAge;
        }


    }


}



Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.