IXmlSerializable Interfaz
Definición
Proporciona un formato personalizado para la serialización y deserialización XML.Provides custom formatting for XML serialization and deserialization.
public interface class IXmlSerializable
public interface IXmlSerializable
type IXmlSerializable = interface
Public Interface IXmlSerializable
- Derivado
Ejemplos
En el ejemplo de código siguiente se muestra una implementación de la IXmlSerializable interfaz que serializa un campo privado.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);
}
}
Comentarios
Hay dos razones para implementar esta interfaz.There are two reasons to implement this interface. La primera es controlar el modo en que el objeto se serializa o deserializa en XmlSerializer .The first is to control how your object is serialized or deserialized by the XmlSerializer. Por ejemplo, puede fragmentar los datos en bytes en lugar de almacenar en búfer conjuntos de datos grandes, y también evitar la inflación que se produce cuando los datos se codifican utilizando la codificación 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. Para controlar la serialización, implemente ReadXml los WriteXml métodos y para controlar XmlReader las XmlWriter clases y utilizadas para leer y escribir 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. Para obtener un ejemplo de esto, vea Cómo: fragmentar datos serializados.For an example of this, see How to: Chunk Serialized Data.
La segunda razón es poder controlar el esquema.The second reason is to be able to control the schema. Para habilitarlo, debe aplicar XmlSchemaProviderAttribute al tipo Serializable y especificar el nombre del miembro estático que devuelve el esquema.To enable this, you must apply the XmlSchemaProviderAttribute to the serializable type, and specify the name of the static member that returns the schema. Vea XmlSchemaProviderAttribute para obtener un ejemplo.See the XmlSchemaProviderAttribute for an example.
Una clase que implementa la interfaz debe tener un constructor sin parámetros.A class that implements the interface must have a parameterless constructor. Este es un requisito de la XmlSerializer clase.This is a requirement of the XmlSerializer class.
Métodos
GetSchema() |
Este método está reservado y no debe utilizarse.This method is reserved and should not be used. Al implementar la interfaz |
ReadXml(XmlReader) |
Genera un objeto a partir de su representación XML.Generates an object from its XML representation. |
WriteXml(XmlWriter) |
Convierte un objeto en su representación XML.Converts an object into its XML representation. |