2 out of 4 rated this helpful - Rate this topic

IXmlSerializable Interface

Provides custom formatting for XML serialization and deserialization.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)
public interface IXmlSerializable

The IXmlSerializable type exposes the following members.

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library GetSchema This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadXml Generates an object from its XML representation.
Public method Supported by the XNA Framework Supported by Portable Class Library WriteXml Converts an object into its XML representation.
Top

There are two reasons to implement this interface. The first is to control how your object is serialized or deserialized by the XmlSerializer. For example, you can chunk data into bytes instead of buffering large data sets, and also avoid the inflation that occurs when the data is encoded using Base64 encoding. To control the serialization, implement the ReadXml and WriteXml methods to control the XmlReader and XmlWriter classes used to read and write the XML. For an example of this, see How To: Chunk Serialized Data.

The second reason is to be able to control the schema. To enable this, you must apply the XmlSchemaProviderAttribute to the serializable type, and specify the name of the static member that returns the schema. See the XmlSchemaProviderAttribute for an example.

A class that implements the interface must have a parameterless constructor. This is a requirement of the XmlSerializer class.

The following example code shows an implementation of the IXmlSerializable interface that serializes a private field.


using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;


public class Person : IXmlSerializable
{

    // Private state

    private string personName;


    // Constructors

    public Person (string name)
    {
        personName = name;
    }

    public Person ()
    {
        personName = null;
    }


    // Xml Serialization Infrastructure

    public void WriteXml (XmlWriter writer)
    {
        writer.WriteString(personName);
    }

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

    public XmlSchema GetSchema()
    {
        return(null);
    }


    // Print

    public override string ToString()
    {
        return(personName);
    }

}


.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

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Existing Implementations
Many of these interfaces are very useful if they've already been implemented by some .NET data structure (DataSet, for example). Why do these interface documentation pages not contain lists of existing implementations?
Bug in IXmlSerializable example?
In the IXmlSerializable example, XmlReader.ReadString() causes unexpected failures. If you are reading multiple xml items (for example, a list of Person rather than a single Person), then the example code will cause the XmlReader to stop after reading the first item. This is apparently because the reader stops at the EndElement. To fix the example, use

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

or

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