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. 此示例,请参阅如何:序列化数据进行分块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),如果需要指定自定义架构,应向该类应用 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) 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.

适用于

另请参阅