IXmlSerializable Interface
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Provides custom formatting for XML serialization and deserialization.

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

public interface IXmlSerializable
public interface class IXmlSerializable
type IXmlSerializable = interface end
Public Interface IXmlSerializable
NameDescription
System_CAPS_pubmethod 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.

System_CAPS_pubmethod ReadXml

Generates an object from its XML representation.

System_CAPS_pubmethod WriteXml

Converts an object into its XML representation.

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

}
#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::Xml::Serialization;
public ref class Person: public IXmlSerializable
{
private:

   // Private state
   String^ personName;

public:

   // Constructors
   Person( String^ name )
   {
      personName = name;
   }

   Person()
   {
      personName = nullptr;
   }

   // Xml Serialization Infrastructure
   virtual void WriteXml( XmlWriter^ writer )
   {
      writer->WriteString( personName );
   }

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

   virtual XmlSchema^ GetSchema()
   {
      return nullptr;
   }

   // Print
   virtual String^ ToString() override
   {
      return (personName);
   }
};
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