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. 有关此操作的示例,请参阅 how to:组块串行化 Data。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. 有关示例,请参见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. |