XmlSerializer::Deserialize Method (Stream^)

 

Deserializes the XML document contained by the specified Stream.

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

public:
Object^ Deserialize(
	Stream^ stream
)

Parameters

stream
Type: System.IO::Stream^

The Stream that contains the XML document to deserialize.

Return Value

Type: System::Object^

The Object being deserialized.

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:

System_CAPS_noteNote

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

The following example deserializes an object using a Stream object.

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

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

// This is the class that will be deserialized.
public ref class OrderedItem
{
public:

   [XmlElement(Namespace="http://www.cpandl.com")]
   String^ ItemName;

   [XmlElement(Namespace="http://www.cpandl.com")]
   String^ Description;

   [XmlElement(Namespace="http://www.cohowinery.com")]
   Decimal UnitPrice;

   [XmlElement(Namespace="http://www.cpandl.com")]
   int Quantity;

   [XmlElement(Namespace="http://www.cohowinery.com")]
   Decimal LineTotal;

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

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

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

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

	// Reading the XML document requires a FileStream.
	Stream^ reader = gcnew FileStream(filename, FileMode::Open);

	try
	{
		// Call the Deserialize method to restore the object's state.
		i = dynamic_cast<OrderedItem^>(serializer->Deserialize(reader));
	}
	finally
	{
		delete 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" );
}
<?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>

Universal Windows Platform
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
Return to top
Show: