13 Mart 2018 Salı

IXmlSerializable Arayüzü

Giriş
Bu sınıfın kardeşi ISerializable Arayüzü.

GetSchemema metodu
null dönmeli. Açıklaması şöyle.
IXmlSerializable.GetSchema Method This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return a null reference (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class.

ReadXml metodu
Açıklaması şöyle.
The ReadXml method must reconstitute your object using the information that was written by the WriteXml method.
When this method is called, the reader is positioned at the start of the element that wraps the information for your type. That is, just before the start tag that indicates the beginning of a serialized object. When this method returns, it must have read the entire element from beginning to end, including all of its contents. Unlike the WriteXml method, the framework does not handle the wrapper element automatically. Your implementation must do so. Failing to observe these positioning rules may cause code to generate unexpected runtime exceptions or corrupt data.
WriteXml metodu
Açıklaması şöyle.
The WriteXml implementation you provide should write out the XML representation of the object. The framework writes a wrapper element and positions the XML writer after its start. Your implementation may write its contents, including child elements. The framework then closes the wrapper element.
Örnek
Elimizde şöyle bir kod olsun
public class XmlTypeProxy : IXmlSerializable {
  private string _typeName;

  public XmlTypeProxy() {

  }

  public XmlTypeProxy(string typeName) {
    _typeName = typeName;
  }

  public XmlSchema GetSchema() {
    return null;
  }

  public void ReadXml(XmlReader reader) {
    _typeName = reader.ReadString();
  }

  public void WriteXml(XmlWriter writer) {
    writer.WriteString(_typeName);
  }

  public static implicit operator Type(XmlTypeProxy self) {
    return Type.GetType(self._typeName);
  }

  public static implicit operator XmlTypeProxy(Type self) {
    return new XmlTypeProxy(self.AssemblyQualifiedName);
  }
}
Şöyle yaparız.
public class SomeObject {
  [XmlElement(Type = typeof(XmlTypeProxy))]
  public Type SomeType { get; set; }
}
Diğer
Normal Kullanım
Bu sınıf normalde XmlReader ve XmlWriter tarafından kullanılır.
Örnek
Elimizde şöyle bir kod olsun.
public class Employee : IXmlSerializable
{
  ...
}
Şöyle yaparız.
using(var file = File.Create("file.xml"))
using(var writer = XmlWriter.Create(file, settings))
{
    serializer.Serialize(writer, employee);
}
Anotasyon Tabanlı Kullanım
Bu sınıf anotasyon tabanlı XmlSerializer ile de kullanılabilir. Elimizde şöyle bir kod olsun.
[XmlRoot("Foo")] 
public sealed class ClassSerializerProxy : IXmlSerializable
{
  public System.Xml.Schema.XmlSchema GetSchema()   {return null;}
  public void WriteXml(System.Xml.XmlWriter writer){...}
  public void ReadXml(System.Xml.XmlReader reader) {...}
  ...
}
Şöyle yaparız.
XmlSerializer xmlSerializer = new XmlSerializer(ClassSerializerProxy);
using (XmlReader reader = xmlDocument.CreateReader())
  return xmlSerializer.Deserialize(reader);


Hiç yorum yok:

Yorum Gönder