views:

856

answers:

2

I would like to implement IXmlSerializable on a class and only override either ReadXml or WriteXml, but not both. If I didn't implement IXMLSerializable on this class, the XMLSerializer would automatically serialize all members. I'd like that default behavior to apply for the non-overridden case. However, since IXmlSerializable is an interface, and not a base class, I'm not sure how to go about that.

In addition, I need to, in one case, do the default behavior, and when that is complete add some extra code. So, I'd like to override and call back to the 'base' class behavior. Again, this would be trivial if there was a base class, but not so trivial since this is an interface.

Thanks!

+1  A: 

You will need to implement both methods, but that is the full extent of your responsibility (as far as the compiler is concerned). You can return null or throw an exception in the method that you will not be using.

EDIT

I'd like the default behavior to apply for the non-overridden case.

What default behavior are you referring to? Since you are implementing an interface, there is no default behavior.

Kyle Trauberman
If I didn't implement IXMLSerializable on this class, the XMLSerializer would automatically serialize all members. That is the 'default' behavior I'm looking for.
Eric
You could throw an NotSupportedException in the method that is not supported
Jhonny D. Cano -Leftware-
+3  A: 

The ability to perform actions before/after serialisation/deserialisation is provided with attributes. Mark a method with OnDeserializedAttribute for it to be called after an instance has been deserialised, just ensure the method has the right signature:

[OnDeserializedAttribute()]
private void RunThisMethod(StreamingContext context) { 
  // ...
}

NB. This attribute works for Binary, SOAP and DataAttribute formatters, but not for XmlSerializer. There is no attribute or mechanism other than implementing IXmlSerializable.

Also do not forget that you can read XML documents directly and write code to (de)serialize.


Original answer: If you need to completely override one of serialisation or deserialisation (and thus implement IXmlSerializable then you have to do both yourself.

It may be possible to make use of attributes and other mechanisms to avoid using IXmlSerializable, could you expand the question with details of why you need to implement just one of ReadXml or WriteXml.

Richard
It is a strange need: At the end of deserialization of certain classes I need to register the newly created instances. So, I was hoping to: (1) Leave serialization as it is, and (2) Override deserialization and add my registration code.
Eric
SO you need an equivalent to OnDeserializedAttribute that applies to the XML Serialiser?
Richard
OnDeserialized will do the trick! Copy this up into the text and I'll mark this as answered.
Eric
Note that XmlSerializer doesn't support this attribute; only DataContractSerializer (and IIRC BinaryFormatter etc)
Marc Gravell
@Marc: As I found this morning when I tried it :-(. Answer corrected.
Richard