views:

46

answers:

1

I need to XML serialize some classes, which in some cases do not adhere to the rules and guidelines, that is needed in order to make built-in serialization work. This includes properties that are interface types and properties without setters.

I know how to implement IXmlSerializable, but it could be a lot of work; since most of the properties will need no special handling. Is there any way for me to only write code to handle these "special properties" and have the standard XML serializer serialize the properties that it knows how to serialize ?

Also, suggestions for open source libraries or similar, that can do a better job at serializing the objects to XML without too much custom code, is welcomed.

+1  A: 

There is a tool called Automapper This tool maps two types to each other. For your problem you could build a standard conform type and map the not standard one to this one. Most of the properties will be mapped with the automapper. Special ones you could map with your own methods.

First you have to setup your mappings:

Mapper.CreateMap<ITestSequence, TestSequence>();
Mapper.CreateMap<ITestBlock, TestBlock>();

And then map your opjects:

Mapper.Map(srcTb, dstTb);

which will set all common properties. The rest you could do by yourself.

schoetbi