IXmlSerializable Interface
Provides custom formatting for XML serialization and deserialization.
Assembly: System.Xml (in System.Xml.dll)
The IXmlSerializable type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | GetSchema | This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return Nothing (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class. |
![]() ![]() ![]() | ReadXml | Generates an object from its XML representation. |
![]() ![]() ![]() | 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.
Imports System Imports System.Xml Imports System.Xml.Schema Imports System.Xml.Serialization Public Class Person Implements IXmlSerializable ' Private state Private personName As String ' Constructors Public Sub New(ByVal name As String) personName = name End Sub Public Sub New() personName = Nothing End Sub ' Xml Serialization Infrastructure Public Sub WriteXml(ByVal writer As XmlWriter) Implements IXmlSerializable.WriteXml writer.WriteString(personName) End Sub Public Sub ReadXml(ByVal reader As XmlReader) Implements IXmlSerializable.ReadXml personName = reader.ReadString() End Sub Public Function GetSchema() As XmlSchema Implements IXmlSerializable.GetSchema Return (Nothing) End Function ' Print Public Overrides Function ToString() As String Return (personName) End Function End Class
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.


