DataContractSerializer.ReadObject DataContractSerializer.ReadObject DataContractSerializer.ReadObject DataContractSerializer.ReadObject Method

Definition

Reads the XML stream and returns the deserialized object.

Overloads

ReadObject(XmlReader) ReadObject(XmlReader) ReadObject(XmlReader) ReadObject(XmlReader)

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

ReadObject(XmlDictionaryReader, Boolean) ReadObject(XmlDictionaryReader, Boolean) ReadObject(XmlDictionaryReader, Boolean) 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) ReadObject(XmlReader, Boolean) ReadObject(XmlReader, Boolean) 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) ReadObject(XmlDictionaryReader, Boolean, DataContractResolver) 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) ReadObject(XmlReader) ReadObject(XmlReader) ReadObject(XmlReader)

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);
override this.ReadObject : System.Xml.XmlReader -> obj
Public Overrides Function ReadObject (reader As XmlReader) As Object

Parameters

reader
XmlReader XmlReader XmlReader XmlReader

The XmlReader used to read the XML stream.

Returns

The deserialized object.

ReadObject(XmlDictionaryReader, Boolean) ReadObject(XmlDictionaryReader, Boolean) ReadObject(XmlDictionaryReader, Boolean) 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.

public:
 override System::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

verifyObjectName
Boolean Boolean Boolean 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 

ReadObject(XmlReader, Boolean) ReadObject(XmlReader, Boolean) ReadObject(XmlReader, Boolean) 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.

public:
 override System::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 XmlReader XmlReader XmlReader

The XmlReader used to read the XML stream.

verifyObjectName
Boolean Boolean Boolean 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.

ReadObject(XmlDictionaryReader, Boolean, DataContractResolver) ReadObject(XmlDictionaryReader, Boolean, DataContractResolver) 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.

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);
override this.ReadObject : System.Xml.XmlDictionaryReader * bool * System.Runtime.Serialization.DataContractResolver -> obj

Parameters

reader
XmlDictionaryReader XmlDictionaryReader XmlDictionaryReader XmlDictionaryReader

The XML reader used to read the content.

verifyObjectName
Boolean Boolean Boolean Boolean

true to verify the object name; otherwise, false.

dataContractResolver
DataContractResolver DataContractResolver DataContractResolver DataContractResolver

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

Returns

The deserialized object.

Applies to