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. 第一個是控制如何將物件序列化或還原序列化XmlSerializerThe 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. 這個範例,請參閱How to:區塊序列化資料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.

適用於

另請參閱