XmlSerializer.Deserialize Method

Definition

Deserializes an XML document.

Overloads

Deserialize(Stream)

Deserializes the XML document contained by the specified Stream.

Deserialize(TextReader)

Deserializes the XML document contained by the specified TextReader.

Deserialize(XmlSerializationReader)

Deserializes the XML document contained by the specified XmlSerializationReader.

Deserialize(XmlReader)

Deserializes the XML document contained by the specified XmlReader.

Deserialize(XmlReader, String)

Deserializes the XML document contained by the specified XmlReader and encoding style.

Deserialize(XmlReader, XmlDeserializationEvents)

Deserializes an XML document contained by the specified XmlReader and allows the overriding of events that occur during deserialization.

Deserialize(XmlReader, String, XmlDeserializationEvents)

Deserializes the object using the data contained by the specified XmlReader.

Deserialize(Stream)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

Deserializes the XML document contained by the specified Stream.

public object Deserialize (System.IO.Stream stream);
public object? Deserialize (System.IO.Stream stream);

Parameters

stream
Stream

The Stream that contains the XML document to deserialize.

Returns

The Object being deserialized.

Examples

The following example deserializes an object using a Stream object.

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

// This is the class that will be deserialized.
public class OrderedItem
{
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string ItemName;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string Description;
    [XmlElement(Namespace="http://www.cohowinery.com")]
    public decimal UnitPrice;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public int Quantity;
    [XmlElement(Namespace="http://www.cohowinery.com")]
    public decimal LineTotal;
    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}

public class Test
{
    public static void Main()
    {
        Test t = new Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

    private void DeserializeObject(string filename)
    {
        Console.WriteLine("Reading with Stream");
        // Create an instance of the XmlSerializer.
        XmlSerializer serializer =
        new XmlSerializer(typeof(OrderedItem));

        // Declare an object variable of the type to be deserialized.
        OrderedItem i;

        using (Stream reader = new FileStream(filename, FileMode.Open))
        {
            // Call the Deserialize method to restore the object's state.
            i = (OrderedItem)serializer.Deserialize(reader);
        }

        // Write out the properties of the object.
        Console.Write(
        i.ItemName + "\t" +
        i.Description + "\t" +
        i.UnitPrice + "\t" +
        i.Quantity + "\t" +
        i.LineTotal);
    }
}
<?xml version="1.0"?>
 <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
   <inventory:ItemName>Widget</inventory:ItemName>
   <inventory:Description>Regular Widget</inventory:Description>
   <money:UnitPrice>2.3</money:UnitPrice>
   <inventory:Quantity>10</inventory:Quantity>
   <money:LineTotal>23</money:LineTotal>
 </OrderedItem>

Remarks

Deserialization is the process of reading an XML document and constructing an object that is strongly typed to the XML Schema (XSD) of the document.

Before deserializing, an XmlSerializer must be constructed using the type of the object that is being deserialized.

Use the stream parameter to specify an object that derives from the Stream class, which is designed to write to streams. Classes that derive from the Stream class include:

Notes

The XmlSerializer cannot deserialize the following: arrays of ArrayList and arrays of List<T>.

See also

Applies to

.NET 9 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Deserialize(TextReader)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

Deserializes the XML document contained by the specified TextReader.

public object Deserialize (System.IO.TextReader textReader);
public object? Deserialize (System.IO.TextReader textReader);

Parameters

textReader
TextReader

The TextReader that contains the XML document to deserialize.

Returns

The Object being deserialized.

Exceptions

An error occurred during deserialization. The original exception is available using the InnerException property.

Examples

The following example deserializes an object using a TextReader object.

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

// This is the class that will be deserialized.
public class OrderedItem
{
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string ItemName;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string Description;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal UnitPrice;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public int Quantity;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal LineTotal;
    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}

public class Test
{
   public static void Main()
    {
        Test t = new Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

    private void DeserializeObject(string filename)
    {
        Console.WriteLine("Reading with TextReader");

        // Create an instance of the XmlSerializer specifying type.
        XmlSerializer serializer =
        new XmlSerializer(typeof(OrderedItem));

        // Create a TextReader to read the file.
        FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
        TextReader reader = new StreamReader(fs);

        // Declare an object variable of the type to be deserialized.
        OrderedItem i;

        // Use the Deserialize method to restore the object's state.
        i = (OrderedItem) serializer.Deserialize(reader);

        // Write out the properties of the object.
        Console.Write(
            i.ItemName + "\t" +
            i.Description + "\t" +
            i.UnitPrice + "\t" +
            i.Quantity + "\t" +
            i.LineTotal);
    }
}

Remarks

Deserialization is the process of reading an instance of an XML document and constructing an object that is strongly typed to the XML Schema (XSD) of the document.

Before deserializing, an XmlSerializer must be constructed using the type of the object that is being deserialized.

Classes that inherit from TextReader include StringReader and StreamReader. If you are using a StreamReader to deserialize an object, you must construct the StreamReader with an appropriate Encoding. The encoding specified by the XML document is ignored.

Notes

To use the encoding specified by the XML document, use the Deserialize overload that takes an XmlReader instead. The XmlReader automatically detects and uses the encoding specified by the XML document.

Notes

The XmlSerializer cannot deserialize the following: arrays of ArrayList and arrays of List<T>.

See also

Applies to

.NET 9 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Deserialize(XmlSerializationReader)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

Deserializes the XML document contained by the specified XmlSerializationReader.

protected virtual object Deserialize (System.Xml.Serialization.XmlSerializationReader reader);

Parameters

reader
XmlSerializationReader

The XmlSerializationReader that contains the XML document to deserialize.

Returns

The deserialized object.

Exceptions

Any attempt is made to access the method when the method is not overridden in a descendant class.

Applies to

.NET 9 et autres versions
Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Deserialize(XmlReader)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

Deserializes the XML document contained by the specified XmlReader.

public object Deserialize (System.Xml.XmlReader xmlReader);
public object? Deserialize (System.Xml.XmlReader xmlReader);

Parameters

xmlReader
XmlReader

The XmlReader that contains the XML document to deserialize.

Returns

The Object being deserialized.

Exceptions

An error occurred during deserialization. The original exception is available using the InnerException property.

Examples

The following example deserializes an object using an XmlReader.

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be deserialized.
public class OrderedItem
{
    public string ItemName;
    public string Description;
    public decimal UnitPrice;
    public int Quantity;
    public decimal LineTotal;

    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}
public class Test
{
    public static void Main(string[] args)
    {
        Test t = new Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

    private void DeserializeObject(string filename)
    {
        Console.WriteLine("Reading with XmlReader");

        // Create an instance of the XmlSerializer specifying type and namespace.
        XmlSerializer serializer = new
        XmlSerializer(typeof(OrderedItem));

        // A FileStream is needed to read the XML document.
        FileStream fs = new FileStream(filename, FileMode.Open);
        XmlReader reader = XmlReader.Create(fs);

        // Declare an object variable of the type to be deserialized.
        OrderedItem i;

        // Use the Deserialize method to restore the object's state.
        i = (OrderedItem)serializer.Deserialize(reader);
        fs.Close();

        // Write out the properties of the object.
        Console.Write(
        i.ItemName + "\t" +
        i.Description + "\t" +
        i.UnitPrice + "\t" +
        i.Quantity + "\t" +
        i.LineTotal);
    }
}
<?xml version="1.0"?>
 <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
   <inventory:ItemName>Widget</inventory:ItemName>
   <inventory:Description>Regular Widget</inventory:Description>
   <money:UnitPrice>2.3</money:UnitPrice>
   <inventory:Quantity>10</inventory:Quantity>
   <money:LineTotal>23</money:LineTotal>
 </OrderedItem>

Remarks

Deserialization is the process of reading an instance of an XML document and constructing an object that is strongly typed to the XML Schema (XSD) of the document.

Before deserializing, an XmlSerializer must be constructed using the type of the object that is being deserialized.

The XmlReader automatically detects and uses the encoding specified by the XML document.

Notes

The XmlSerializer cannot deserialize the following: arrays of ArrayList and arrays of List<T>.

See also

Applies to

.NET 9 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Deserialize(XmlReader, String)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

Deserializes the XML document contained by the specified XmlReader and encoding style.

public object? Deserialize (System.Xml.XmlReader xmlReader, string? encodingStyle);
public object Deserialize (System.Xml.XmlReader xmlReader, string encodingStyle);

Parameters

xmlReader
XmlReader

The XmlReader that contains the XML document to deserialize.

encodingStyle
String

The encoding style of the serialized XML.

Returns

The deserialized object.

Exceptions

An error occurred during deserialization. The original exception is available using the InnerException property.

Remarks

Deserialization is the process of reading an instance of an XML document and constructing an object that is strongly typed to the XML Schema (XSD) of the document.

Before deserializing, an XmlSerializer must be constructed using the type of the object that is being deserialized.

Set the encodingStyle parameter to "http://schemas.xmlsoap.org/soap/encoding/" for SOAP version 1.1 encoding; otherwise, set it to "http://www.w3.org/2001/12/soap-encoding" for SOAP version 1.2 encoding.

Note The XmlSerializer cannot deserialize the following: arrays of ArrayList and arrays of List<T>.

See also

Applies to

.NET 9 et autres versions
Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Deserialize(XmlReader, XmlDeserializationEvents)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

Deserializes an XML document contained by the specified XmlReader and allows the overriding of events that occur during deserialization.

public object? Deserialize (System.Xml.XmlReader xmlReader, System.Xml.Serialization.XmlDeserializationEvents events);
public object Deserialize (System.Xml.XmlReader xmlReader, System.Xml.Serialization.XmlDeserializationEvents events);

Parameters

xmlReader
XmlReader

The XmlReader that contains the document to deserialize.

events
XmlDeserializationEvents

An instance of the XmlDeserializationEvents class.

Returns

The Object being deserialized.

Remarks

The object being deserialized.

Applies to

.NET 9 et autres versions
Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Deserialize(XmlReader, String, XmlDeserializationEvents)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

Deserializes the object using the data contained by the specified XmlReader.

public object? Deserialize (System.Xml.XmlReader xmlReader, string? encodingStyle, System.Xml.Serialization.XmlDeserializationEvents events);
public object Deserialize (System.Xml.XmlReader xmlReader, string encodingStyle, System.Xml.Serialization.XmlDeserializationEvents events);

Parameters

xmlReader
XmlReader

An instance of the XmlReader class used to read the document.

encodingStyle
String

The encoding used.

events
XmlDeserializationEvents

An instance of the XmlDeserializationEvents class.

Returns

The object being deserialized.

Remarks

This method is required for deserialization of unknown headers for Web Service scenarios only. This method allows you to avoid event synchronization in Web Service methods.

Applies to

.NET 9 et autres versions
Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1