Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
IXmlSerializable Interface

Provides custom formatting for XML serialization and deserialization.

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

Visual Basic (Declaration)
Public Interface IXmlSerializable
Visual Basic (Usage)
Dim instance As IXmlSerializable
C#
public interface IXmlSerializable
C++
public interface class IXmlSerializable
J#
public interface IXmlSerializable
JScript
public interface IXmlSerializable

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.

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

}
C++
#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);
   }
};
J#
import System.*;
import System.Xml.*;
import System.Xml.Schema.*;
import System.Xml.Serialization.*;

public class Person implements IXmlSerializable
{
    // Private state
    private String personName;

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

    public Person()
    {
        personName = null;
    } //Person

    // Xml Serialization Infrastructure
    public void WriteXml(XmlWriter writer)
    {
        writer.WriteString(personName);
    } //WriteXml
    public void ReadXml(XmlReader reader)
    {
        personName = reader.ReadString();
    } //ReadXml

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

    // Print
    public String ToString()
    {
        return personName;
    } //ToString
} //Person 

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Example      KeithBa   |   Edit   |   Show History
A better example can be for the XmlSchemaProviderAttribute class. It's more real world.
Tags What's this?: Add a tag
Flag as ContentBug
Another Example      Patrick Altman   |   Edit   |   Show History

I have posted an example implementation of IXmlSerializable at:

http://paltman.com/archive/2006/07/03/ixmlserializableexample.aspx

 

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker