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'); }
No,
XmlSerializerdoes not support this. If you’re using .NET 3.0 or later, take a look at theDataContractSerializer.