Skip to main content
.NET Framework Class Library
IXmlSerializable..::.ReadXml Method

Generates an object from its XML representation.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in System.Xml.dll)
Syntax
Sub ReadXml ( _
	reader As XmlReader _
)
void ReadXml(
	XmlReader reader
)
void ReadXml(
	XmlReader^ reader
)
abstract ReadXml : 
        reader:XmlReader -> unit 

Parameters

reader
Type: System.Xml..::.XmlReader
The XmlReader stream from which the object is deserialized.
Remarks

The ReadXml method must reconstitute your object using the information that was written by the WriteXml method.

When this method is called, the reader is positioned at the start of the element that wraps the information for your type. That is, just before the start tag that indicates the beginning of a serialized object. When this method returns, it must have read the entire element from beginning to end, including all of its contents. Unlike the WriteXml method, the framework does not handle the wrapper element automatically. Your implementation must do so. Failing to observe these positioning rules may cause code to generate unexpected runtime exceptions or corrupt data.

When implementing this method, you should consider the possibility that a malicious user might provide a well-formed but invalid XML representation in order to disable or otherwise alter the behavior of your application.

Examples

The following example illustrates an implementation of the ReadXml method.


Public Sub ReadXml(ByVal reader As XmlReader) Implements IXmlSerializable.ReadXml
    personName = reader.ReadString()
End Sub


public void ReadXml (XmlReader reader)
{
    personName = reader.ReadString();
}


virtual void ReadXml( XmlReader^ reader )
{
   personName = reader->ReadString();
}

The following example illustrates the use of the XmlSerializer class to deserialize this object.


Imports System
Imports System.IO
Imports System.Xml.Serialization

Public Class Reader

  Public Shared Sub Main()
	Dim serializer As New XmlSerializer(GetType(Person))
	Dim file As New FileStream("test.xml", FileMode.Open)
	Dim aPerson As Person = CType(serializer.Deserialize(file), Person)
	Console.WriteLine(aPerson)
  End Sub

End Class


using System;
using System.IO;
using System.Xml.Serialization;

public class Reader {

  public static void Main() {
    XmlSerializer serializer = new XmlSerializer(typeof(Person));
    FileStream file = new FileStream("test.xml", FileMode.Open);
    Person aPerson = (Person) serializer.Deserialize(file);
    Console.WriteLine(aPerson);
  }

}


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

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

int main()
{
   XmlSerializer^ serializer = gcnew XmlSerializer( Person::typeid );
   FileStream^ file = gcnew FileStream( "test.xml",FileMode::Open );
   Person^ aPerson = dynamic_cast<Person^>(serializer->Deserialize( file ));
   Console::WriteLine( aPerson );
}

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.