2

Otherwise than pointed out in this post I would like to act just before an XmlSerializer starts deserializing (not when finished deserializing)

Background: I have a baseClass that implements INotifyPropertyChanged. This BaseClass is stored as xml in a database and when retrieved deserialized into an object instance. The deserialization executes the setters of this class where my ChangeNotification takes place. On a centralised handler for the changenotification I set the status of the object and keep track of stacks for undoing changes. Apparently I don't want these to be trigerred during deserialization.

Any ideas would be very welcome!

Community
  • 1
  • 1
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59

2 Answers2

2

XmlSerializer doesn't support serialization callbacks. One option is to use IXmlSerializable, but that is lots of work. In some simple cases DataContractSerializer may be a viable option; it supports the cllbacks, but it does not support as many XML scenarios (most notably: no attributes).

You may be out of luck, in which case consider a separate DTO and domain type. For example, you could deserialize into FooDto, then copy the values into a Foo, manually telling it that this is via serialization.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I know it doesn't support serialization callbacks...Why is using IXmlSerializable lots of work. It's a pretty straightforward interface? And...do you copy paste this answer? Because I came across it multiple times already...What I actually am looking for is a solution as in the post I mentioned, but then for a deserializing scenario instead of deserialized... – Youp Bernoulli Mar 07 '11 at 15:07
  • @Joep well, the facts haven't changed, and aren't likely to change any time soon. And no, I didn't copy it, thanks. And sorry, but `IXmlSerializable` *is not* (IMO) a simple interface (*particularly* during deserialization). If you can write an implementation for a non-trivial object-tree, that correctly handles all combinations of nodes, empty nodes, subtrees, unexpected data, missing data, etc - then good luck to you. Since you find it so easy, go do that, and just toggle `IsDeserializing` (or whatever) either side of that. – Marc Gravell Mar 07 '11 at 15:15
  • thanx for the support, I was a bit too aggressive in the first place. I think I move towards the most pragmatic solution to create a method that I call manually that puts things to a kind of defaulted state after deserialization (emptying the undo stack, setting the status of the object to unchanged, etc.) – Youp Bernoulli Mar 08 '11 at 08:38
1

As commented @ Marc's solution. The most pragmatic way right now (I'm developing a proof of concept for a big chunk of complex functionality) is to default the object after it has been deserialized with a simple public void. In this method I can empty the undo stack and set the status of the object to unchanged.

If I come up with some better ideas I'll post them here.

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59