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

}

注解

实现此接口有两个原因。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. 若要控制序列化, 请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.

第二个原因是能够控制架构。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()

此方法是保留方法,请不要使用。This method is reserved and should not be used. 在实现 IXmlSerializable 接口时,应从此方法返回 null(在 Visual Basic 中为 Nothing),如果需要指定自定义架构,应向该类应用 XmlSchemaProviderAttributeWhen 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)

从对象的 XML 表示形式生成该对象。Generates an object from its XML representation.

WriteXml(XmlWriter)

将对象转换为其 XML 表示形式。Converts an object into its XML representation.

适用于

另请参阅