views:

272

answers:

2

I'm working with a list of fonts that I serialize and deserialize using DataContractSerializer. In between the two steps, it's conceivable that the user has removed a font from their machine. I'd like to check a font name as it's being deserialized to ensure that it still exists on the system. If it doesn't exist, that element is not included in the collection returned by DataContractSerializer.ReadObject().

Specifically, I'm storing a FontFamily and serializing a property that gets FontFamily.Name. In this property's set accessor, I convert the string back into a FontFamily.

The only reasonable alternative to validation that I can think of would be having the property's set accessor ignore invalid values, and filter out the invalid deserialized objects later. I don't like this option, however - is there a more proper way?

+3  A: 

Why not take advantage on the OnDeserializedAttribute? Have your callback do the validation and removal of items that are not valid for the client environment.

http://msdn.microsoft.com/en-us/library/ms733734.aspx

I do have some concerns about how you would go about round tripping the data if you remove or modify the data under the covers.

(For example: I remember being particularly frustrated by older versions of MS Publisher as I was working on a document on two different machines hooked up to two different printers. Whenever I modified the file on one machine, Publisher would be reformat the document to target the printer attached to that machine. When I went back to the other machine where I was going to do the actual printing, Publisher would reformat again, but the margins would not be quite right and so I needed to tweak some more.)

Ants
I guess that would be okay. I still don't like the idea that the invalid items are still created, but I guess I can live with it in this case. Re: data portability, at the moment, I'm not going to worry about making the serialized file usable on multiple machines. Maybe later.
Matthew Maravillas
A: 

You could also implement IXmlSerializable for you class, which would include your own implementation of ReadXml, allowing you to do whatever validation you wanted as the object was being deserialized.

Factor Mystic
While valid, that is a lot of extra work... for XmlSerializer (which doesn't do callbacks) that is the only way, but for DataContractSerializer, the callback route is far preferable.
Marc Gravell