Edit

Share via


DataContractSerializer.ReadObject Method

Definition

Reads the XML stream and returns the deserialized object.

Overloads

ReadObject(XmlReader)

Reads the XML stream with an XmlReader and returns the deserialized object.

ReadObject(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.

ReadObject(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.

ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)

Reads an XML document or document stream and returns the deserialized object. The method includes a parameter to specify whether the object name is verified is validated, and a resolver for mapping xsi:type declarations at runtime.

ReadObject(XmlReader)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Reads the XML stream with an XmlReader and returns the deserialized object.

public:
 override System::Object ^ ReadObject(System::Xml::XmlReader ^ reader);
public override object ReadObject (System.Xml.XmlReader reader);
public override object? ReadObject (System.Xml.XmlReader reader);
override this.ReadObject : System.Xml.XmlReader -> obj
Public Overrides Function ReadObject (reader As XmlReader) As Object

Parameters

reader
XmlReader

The XmlReader used to read the XML stream.

Returns

The deserialized object.

Applies to

ReadObject(XmlDictionaryReader, Boolean)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

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.

public:
 override System::Object ^ ReadObject(System::Xml::XmlDictionaryReader ^ reader, bool verifyObjectName);
public override object ReadObject (System.Xml.XmlDictionaryReader reader, bool verifyObjectName);
public override object? ReadObject (System.Xml.XmlDictionaryReader reader, bool verifyObjectName);
override this.ReadObject : System.Xml.XmlDictionaryReader * bool -> obj
Public Overrides Function ReadObject (reader As XmlDictionaryReader, verifyObjectName As Boolean) As Object

Parameters

reader
XmlDictionaryReader

The XmlDictionaryReader used to read the XML stream.

verifyObjectName
Boolean

true to check whether the name of the object corresponds to the root name value supplied in the constructor; otherwise, false.

Returns

The deserialized object.

Exceptions

The verifyObjectName parameter is set to true, and the element name and namespace do not correspond to the values set in the constructor.

Examples

The following example reads an XML document and deserializes an instance of an object.

public static void ReadObject(string fileName)
{
    Console.WriteLine("Deserializing an instance of the object.");
    FileStream fs = new FileStream(fileName,
    FileMode.Open);
    XmlDictionaryReader reader =
        XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
    DataContractSerializer ser = new DataContractSerializer(typeof(Person));

    // Deserialize the data and read it from the instance.
    Person deserializedPerson =
        (Person)ser.ReadObject(reader, true);
    reader.Close();
    fs.Close();
    Console.WriteLine(String.Format("{0} {1}, ID: {2}",
    deserializedPerson.FirstName, deserializedPerson.LastName,
    deserializedPerson.ID));
}
Public Shared Sub ReadObject(ByVal fileName As String) 
    Console.WriteLine("Deserializing an instance of the object.")
    Dim fs As New FileStream(fileName, FileMode.Open)
    Dim reader As XmlDictionaryReader = _
        XmlDictionaryReader.CreateTextReader(fs, New XmlDictionaryReaderQuotas())
    Dim ser As New DataContractSerializer(GetType(Person))
    
    ' Deserialize the data and read it from the instance.
    Dim deserializedPerson As Person = CType(ser.ReadObject(reader, True), Person)
    reader.Close()
    fs.Close()
    Console.WriteLine(String.Format("{0} {1}, ID: {2}", deserializedPerson.FirstName, deserializedPerson.LastName, deserializedPerson.ID))
End Sub

Applies to

ReadObject(XmlReader, Boolean)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

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.

public:
 override System::Object ^ ReadObject(System::Xml::XmlReader ^ reader, bool verifyObjectName);
public override object ReadObject (System.Xml.XmlReader reader, bool verifyObjectName);
public override object? ReadObject (System.Xml.XmlReader reader, bool verifyObjectName);
override this.ReadObject : System.Xml.XmlReader * bool -> obj
Public Overrides Function ReadObject (reader As XmlReader, verifyObjectName As Boolean) As Object

Parameters

reader
XmlReader

The XmlReader used to read the XML stream.

verifyObjectName
Boolean

true to check whether the name of the object corresponds to the root name value supplied in the constructor; otherwise, false.

Returns

The deserialized object.

Exceptions

The verifyObjectName parameter is set to true, and the element name and namespace do not correspond to the values set in the constructor.

Applies to

ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Reads an XML document or document stream and returns the deserialized object. The method includes a parameter to specify whether the object name is verified is validated, and a resolver for mapping xsi:type declarations at runtime.

public:
 System::Object ^ ReadObject(System::Xml::XmlDictionaryReader ^ reader, bool verifyObjectName, System::Runtime::Serialization::DataContractResolver ^ dataContractResolver);
public object? ReadObject (System.Xml.XmlDictionaryReader reader, bool verifyObjectName, System.Runtime.Serialization.DataContractResolver? dataContractResolver);
public object ReadObject (System.Xml.XmlDictionaryReader reader, bool verifyObjectName, System.Runtime.Serialization.DataContractResolver dataContractResolver);
override this.ReadObject : System.Xml.XmlDictionaryReader * bool * System.Runtime.Serialization.DataContractResolver -> obj
Public Function ReadObject (reader As XmlDictionaryReader, verifyObjectName As Boolean, dataContractResolver As DataContractResolver) As Object

Parameters

reader
XmlDictionaryReader

The XML reader used to read the content.

verifyObjectName
Boolean

true to verify the object name; otherwise, false.

dataContractResolver
DataContractResolver

An implementation of the DataContractResolver to map xsi:type declarations to data contract types.

Returns

The deserialized object.

Applies to