DataContractSerializer.Deserialize Method

System.Runtime.Serialization

Summary

Deserializes an XML stream into an object of the specified type.

Namespace: System.Runtime.Serialization
Assembly: System.Runtime.Serialization (in System.Runtime.Serialization.dll)

Syntax

public object Deserialize (System.Xml.XmlReader reader);
public object Deserialize (System.Xml.XmlReader reader, Type targetType);
public object Deserialize (System.Xml.XmlReader reader, Type targetType, System.Runtime.Serialization.SerializationBinder binder);

Parameters

Name Type Description
reader System.Xml.XmlReader An System.Xml.XmlReader that contains the XML stream to deserialize.
targetType Type (Optional) The type of the object to deserialize. If not specified, the type is inferred from the XML.
binder System.Runtime.Serialization.SerializationBinder (Optional) A System.Runtime.Serialization.SerializationBinder that controls the deserialization process.

Returns

An object of the specified type, or null if the stream is empty or contains no serializable data.

Exceptions

  • System.Runtime.Serialization.SerializationException: Occurs if the XML stream is invalid or cannot be deserialized.
  • System.Xml.XmlException: Occurs if the XML is malformed.
  • System.ArgumentNullException: Occurs if the reader parameter is null.

Remarks

The Deserialize method reads an XML stream from the provided XmlReader and reconstructs the serialized object. The method supports various data types and complex object graphs. If the targetType is not specified, the type of the object is determined from the XML's root element. For more advanced control over type resolution during deserialization, a SerializationBinder can be provided.

Ensure that the XML stream conforms to the contract defined by the serialized object to avoid deserialization errors. The DataContractSerializer is designed to work with data contracts defined using the Data Contract Attributed Programming Model.

Example

The following code example demonstrates how to use the Deserialize method to deserialize an XML stream into a Person object.


using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

// Assume Person class is defined elsewhere with [DataContract] and [DataMember] attributes

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

public class Example
{
    public static void Main()
    {
        string xmlString = @"
        <Person xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MyNamespace"">
            <Age>30</Age>
            <Name>Alice</Name>
        </Person>";

        using (StringReader stringReader = new StringReader(xmlString))
        using (XmlReader xmlReader = XmlReader.Create(stringReader))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(Person));
            try
            {
                Person person = (Person)serializer.Deserialize(xmlReader);
                Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
            }
            catch (SerializationException ex)
            {
                Console.WriteLine($"Serialization error: {ex.Message}");
            }
            catch (XmlException ex)
            {
                Console.WriteLine($"XML error: {ex.Message}");
            }
        }
    }
}