IXmlSerializable IXmlSerializable IXmlSerializable IXmlSerializable Interface

定義

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

}

備註

有兩個原因會執行此介面。There are two reasons to implement this interface. 第一個方法是控制您的物件序列化或還原序列化的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. 若要控制序列化, 請執行ReadXmlWriteXml方法, 以控制XmlReaderXmlWriter來讀取和寫入 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.

第二個原因是要能夠控制架構。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. 如需範例,請參閱 XmlSchemaProviderAttributeSee 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() GetSchema() GetSchema() GetSchema()

這個方法已保留且不應使用。This method is reserved and should not be used. 實作 IXmlSerializable 介面時,您應該從這個方法傳回 null (在 Visual Basic 中為 Nothing),而且如果需要指定自訂結構描述,請改為將 XmlSchemaProviderAttribute 套用至類別。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.

ReadXml(XmlReader) ReadXml(XmlReader) ReadXml(XmlReader) ReadXml(XmlReader)

從物件的 XML 表示產生該物件。Generates an object from its XML representation.

WriteXml(XmlWriter) WriteXml(XmlWriter) WriteXml(XmlWriter) WriteXml(XmlWriter)

將物件轉換成其 XML 表示。Converts an object into its XML representation.

適用於

另請參閱