XmlSerializer::Deserialize Method (TextReader^)
Deserializes the XML document contained by the specified TextReader.
Assembly: System.Xml (in System.Xml.dll)
Parameters
- textReader
-
Type:
System.IO::TextReader^
The TextReader that contains the XML document to deserialize.
| Exception | Condition |
|---|---|
| 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.
Note |
|---|
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. |
Note |
|---|
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" ); }
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
XmlAttributes
CanDeserialize
Serialize
Deserialize Overload
XmlSerializer Class
System.Xml.Serialization Namespace
Introducing XML Serialization
How to: Specify an Alternate Element Name for an XML Stream
Controlling XML Serialization Using Attributes
Examples of XML Serialization
XML Schema Definition Tool (Xsd.exe)
