ReadXml Method
Collapse the table of content
Expand the table of content

IXmlSerializable.ReadXml Method (XmlReader)

 

Generates an object from its XML representation.

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

void ReadXml(
	XmlReader reader
)

Parameters

reader
Type: System.Xml.XmlReader

The XmlReader stream from which the object is deserialized.

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 on the start tag that wraps the information for your type. That is, directly on 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.

The following example illustrates an implementation of the ReadXml method.

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

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

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);
  }

}

Universal Windows Platform
Available since 4.5
.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:
© 2016 Microsoft