Export (0) Print
Expand All

XmlSerializer::Deserialize Method (TextReader)

Deserializes the XML document contained by the specified TextReader.

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

public:
Object^ Deserialize(
	TextReader^ textReader
)

Parameters

textReader
Type: System.IO::TextReader

The TextReader that contains the XML document to deserialize.

Return Value

Type: System::Object
The Object being deserialized.

ExceptionCondition
InvalidOperationException

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

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.

NoteNote

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.

NoteNote

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

The following example deserializes an object using a TextReader object.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Xml::Serialization;

// This is the class that will be deserialized. 
public ref class OrderedItem
{
public:
   String^ ItemName;
   String^ Description;
   Decimal UnitPrice;
   int Quantity;
   Decimal LineTotal;

   // A custom method used to calculate price per item. 
   void Calculate()
   {
      LineTotal = UnitPrice * Quantity;
   }
};

void DeserializeObject( String^ filename )
{
   Console::WriteLine( "Reading with TextReader" );

   // Create an instance of the XmlSerializer specifying type.
   XmlSerializer^ serializer = gcnew XmlSerializer( OrderedItem::typeid );

   /* Create a TextReader to read the file. Specify an
      Encoding to use. */
   TextReader^ reader = gcnew StreamReader( filename,Encoding::Unicode );

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

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

   // Write out the properties of the object.
   Console::Write( "{0}\t{1}\t{2}\t{3}\t{4}", i->ItemName, i->Description, i->UnitPrice, i->Quantity, i->LineTotal );
}

int main()
{
   // Read a purchase order.
   DeserializeObject( "simple.xml" );
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft