Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In .Net/C# is the OnSerializing event fired for XmlSerializer.Serialize

Tags:

c#

.net

I want to set some attributes just before the object is serialized, but as it can be serialized from several locations, is there a way to do this using the OnSerializing method (or similar) for Xml serialization - my class is largely like this - but the On... methods are not being called...:

[Serializable]
[XmlRoot(ElementName = "ResponseDetails", IsNullable = false)]
public class ResponseDetails
{
    public ResponseDetails() {}


    [OnSerializing]
    internal void OnSerializingMethod(StreamingContext context)
    {
        logger.Info("Serializing response");
    }

    [OnSerialized]
    internal void OnSerializedMethod(StreamingContext context)
    {
        logger.Info("Serialized response");
    }

    [OnDeserialized]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        logger.Info("Deserialized response");
    }

    [OnDeserializing]
    internal void OnDeserializingMethod(StreamingContext context)
    {
        logger.Info("Deserializing response");
    }
like image 461
Chris Kimpton Avatar asked Oct 14 '08 08:10

Chris Kimpton


1 Answers

No, XmlSerializer does not support this. If you're using .NET 3.0 or later, take a look at the DataContractSerializer.

like image 57
Kent Boogaart Avatar answered Sep 19 '22 17:09

Kent Boogaart