Wednesday 15 August 2012

c# - Can I provide custom serialization for XmlSerializer without implementing IXmlSerializable? -


We are using XmlSerializer , and I want to give custom serialization for some classes . However, I do not always have the ability to modify the source code of the class in question, otherwise I could apply it to IXmlSerializable Is there any way to do this?

The helpful example of a proxy is deserialize here:

Given a type of that We can not control the serialization directly at the classroom level:

  public seal class class / user example {public string property {get; Set;}}   

and we need to deserialize with XML:

  & lt; Class & gt; & Lt; Property & gt; Price & lt; / Property & gt; & Lt; / Class & gt;   

You can create a proxy type to manually process the target type deserialization process:

  [XmlRoot ("class")] // & lt; - Very Important Public Seal Class ClassSerializerProxy: IXmlSerializable {Public Class ClassValue {Get; Set;} Public System.xml.Schema.XmlSchema GetSchema () {return null;} Public Zero WriteXml (System.Xml.XmlWriter Authors) {} Public Zero ReadXml (System.Xml.XmlReader Reader) {var x = XElement.ReadFrom (Reader) as XElement; This.ClassValue = New orbit (); // Again it is a simple example. Class value Property = x.except sleap ("property"). Values; }}   

is used:

  void main () {// XML value var xdoc = XDocument.Parse (@ "& lt; Class & gt;; & lt; property & gt; value & lt; / asset & gt; & lt; / class & gt; "); // xml = deserialize in proxy type var proxy & lt; ClassSerializerProxy & gt; (Xdoc); // Read the result value var value = proxy.ClassValue; } Public Object Disraeliise (Exudition XMLDoc, Type Decipherless Tooipes) {XML Serializer XML Serializer = New XML Serializer (Decierialize Toupee); XmlSerializer.Deserialize (reader) using (XmlReader Reader = xmlDocument.CreateReader ()); }   

Throw in some generic and an extension method, and we can clean the call site for the final (cutting-edge handling) version: < P> Usage:

  void main () {var xml = @ "class   

Type your example:

  public seal class class {public string property {get; Set;}}   

An interface that needs to be applied to a proxy type

  Public interface iasarielizer proxy & lt; TInstanceType & gt; Where TInstanceType: Class {TInstanceType Value {get; }}   

Example proxy now applies to the new interface (XmlRoot ("class")] Class Seerializer Proxy class seal: IXmlSerializable, ISerializerProxy & LT; Class & gt; {PUBLIC SOCIAL PRICE {MIL; Set;} Public System.Xml.Schema.XmlSchema GetSchema () {return null;} Public Zero WriteXml (System.Xml.XmlWriter Authors) {} Public Zero ReadXml (System.Xml.XmlReader Reader) {Var x = XElement.ReadFrom (Reader) as XElement; this. Value = new orbit (); This.Value.Property = x.XPathSelectElement ("property"). Values; }}

deserialization method is now an extension method on string and it can be used with any proxy type.

  Public static class ExtensionMethods {public static TInstanceType DeserializeWithProxy & LT; TProxyType, TInstanceType & gt; (This string xml) where TProxyType: ISerializerProxy & LT; TInstanceType & gt; Where TInstanceType: using class {XmlReader reader = XDocument.Parse (xml) .CreateReader ()) {var XmlSerializer = New XmlSerializer (typeof (TProxyType)); Return (xmlSerializer.Deserialize (Reader) as ISerializerProxy & lt; TInstanceType & gt;). Values; }}}    

No comments:

Post a Comment