IXmlSerializable Interfejs

Definicja

Zapewnia niestandardowe formatowanie na potrzeby serializacji i deserializacji XML.

public interface class IXmlSerializable
public interface IXmlSerializable
type IXmlSerializable = interface
Public Interface IXmlSerializable
Pochodne

Przykłady

Poniższy przykładowy kod przedstawia implementację interfejsu IXmlSerializable , który serializuje pole prywatne.

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

Uwagi

Istnieją dwa powody implementacji tego interfejsu. Pierwszym z nich jest kontrolowanie sposobu serializacji lub deserializacji obiektu przez obiekt XmlSerializer. Na przykład można podzielić dane na bajty zamiast buforować duże zestawy danych, a także uniknąć inflacji, która występuje, gdy dane są kodowane przy użyciu kodowania Base64. Aby kontrolować serializacji, zaimplementuj ReadXml metody i WriteXml w celu kontrolowania XmlReader klas i XmlWriter używanych do odczytywania i zapisywania kodu XML. Aby zapoznać się z tym przykładem, zobacz Instrukcje: fragment serializowane dane.

Drugim powodem jest możliwość kontrolowania schematu. Aby to umożliwić, należy zastosować element XmlSchemaProviderAttribute do typu możliwego do serializacji i określić nazwę statycznego elementu członkowskiego zwracającego schemat. XmlSchemaProviderAttribute Zobacz przykład.

Klasa, która implementuje interfejs, musi mieć konstruktor bez parametrów. Jest to wymaganie klasy XmlSerializer .

Metody

GetSchema()

Ta metoda jest zarezerwowana i nie należy jej używać. Podczas implementowania interfejsu IXmlSerializable należy zwrócić null wartość (Nothing w Visual Basic) z tej metody, a zamiast tego, jeśli określenie niestandardowego schematu jest wymagane, zastosuj element XmlSchemaProviderAttribute do klasy .

ReadXml(XmlReader)

Generuje obiekt na podstawie jego reprezentacji XML.

WriteXml(XmlWriter)

Konwertuje obiekt na jego reprezentację XML.

Dotyczy

Zobacz też