IXmlSerializable インターフェイス
定義
XML シリアル化および逆シリアル化のカスタム書式を提供します。Provides custom formatting for XML serialization and deserialization.
public interface class IXmlSerializable
public interface IXmlSerializable
type IXmlSerializable = interface
Public Interface IXmlSerializable
- 派生
例
次のコード例は、 IXmlSerializable プライベートフィールドをシリアル化するインターフェイスの実装を示しています。The following example code shows an implementation of the IXmlSerializable interface that serializes a private field.
#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);
}
};
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);
}
}
注釈
このインターフェイスを実装する理由は2つあります。There are two reasons to implement this interface. 1つ目は、によってオブジェクトをシリアル化または逆シリアル化する方法を制御することです XmlSerializer 。The first is to control how your object is serialized or deserialized by the XmlSerializer. たとえば、大きなデータセットをバッファリングするのではなく、データをバイトに分割することができます。また、Base64 エンコードを使用してデータをエンコードするときに発生するインフレも回避できます。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. シリアル化を制御するには、およびメソッドを実装して、 ReadXml WriteXml XmlReader XmlWriter XML の読み取りと書き込みに使用するクラスおよびクラスを制御します。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.
2つ目の理由は、スキーマを制御できるようにするためです。The second reason is to be able to control the schema. これを有効にするには、 XmlSchemaProviderAttribute シリアル化可能な型にを適用し、スキーマを返す静的メンバーの名前を指定する必要があります。To enable this, you must apply the XmlSchemaProviderAttribute to the serializable type, and specify the name of the static member that returns the schema. 例については、「XmlSchemaProviderAttribute」をご覧ください。See the XmlSchemaProviderAttribute for an example.
インターフェイスを実装するクラスには、パラメーターなしのコンストラクターが必要です。A class that implements the interface must have a parameterless constructor. これは、クラスの要件です XmlSerializer 。This is a requirement of the XmlSerializer class.
メソッド
GetSchema() |
このメソッドは予約されているため、使用できません。This method is reserved and should not be used. |
ReadXml(XmlReader) |
XML 表現からオブジェクトを生成します。Generates an object from its XML representation. |
WriteXml(XmlWriter) |
オブジェクトを XML 表現に変換します。Converts an object into its XML representation. |