Overriding XML deserialization to use base deserialization and adding functionality

user271077 picture user271077 · Apr 2, 2012 · Viewed 7.3k times · Source

I have a class which should be serialized and deserialzed.

But every time after deserilization I need to call a method of synchronizing references.

Anyway I can implement the deserialization and use the traditional deserialization but add the call to my method after the regular deserialization?

Answer

avs099 picture avs099 · Apr 4, 2012
using System.Xml.Serialization;

namespace Custom.Xml.Serialization
{
    public interface IXmlDeserializationCallback
    {
        void OnXmlDeserialization(object sender);
    }

    public class CustomXmlSerializer : XmlSerializer
    {
        protected override object Deserialize(XmlSerializationReader reader)
        {
            var result = base.Deserialize(reader);

            var deserializedCallback = result as IXmlDeserializationCallback;
            if (deserializedCallback != null)
            {
                deserializedCallback.OnXmlDeserialization(this);
            }

            return result;
        }
    }
}

inherit your class from IXmlDeserializationCallback and implement your synchronizing logic in OnXmlDeserialization method.

credits to How do you find out when you've been loaded via XML Serialization?

UPDATE:

well, as far as I understand the topicstarter, he does not want to "manually" call some logic after each XML deserialization. So instead of doing this:

public class MyEntity
{
     public string SomeData { get; set; }

     public void FixReferences()
     {
          // call after deserialization
          // ...
     }
}

foreach (var xmlData in xmlArray)
{
    var xmlSer = new XmlSerializer(typeof(MyEntity));
    using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlData)))
    {
       var entity = (MyEntity)xmlSer.Deserialize(memStream);
       entity.FixReferences();

       // do something else with the entity
       // ...  
    }
}

he wants to do just deserialization, without worrying about extra calls. In this case, proposed solution is the cleanest / simplest - you only need to inherit your entity class from IXmlDeserializationCallback interface, and replace XmlSerializer with CustomXmlSerializer:

 public class MyEntity: IXmlDeserializationCallback
    {
         public string SomeData { get; set; }

         private void FixReferences()
         {
              // call after deserialization
              // ...
         }

         public void OnXmlDeserialization(object sender)
         {
             FixReferences();
         } 
    }

    foreach (var xmlData in xmlArray)
    {
        var xmlSer = new CustomXmlSerializer(typeof(MyEntity));
        using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlData)))
        {
           var entity = (MyEntity)xmlSer.Deserialize(memStream);
           // entity.FixReferences(); - will be called automatically

           // do something else with the entity
           // ...  
        }
    }