views:

59

answers:

1

Hi,

I've a requirement to serialize any class provided (decorated with the appropriate XmlElement/ XmlAttribute etc), but some of the properties may contain some sort of mark-up...usually HTML, but it could as easily be XML in the future.

When trying to serialize the class the XmlSerializer crashes.

I'd hope to be able to apply no more than an attribute to the property (currently set to XmlText) in the hope that it would wrap the content in CDATA[...], but that doesn't seem to work.

I've seen several 'workarounds' like the one here, but I'd hoped for something a little less impactful for the developing consumer.

Does anyone know of any 'nicer' less invasive solution...?

Thanks, Kieron

Edit:

Based on Damian's answer, I found this article which shows the differences between the XmlSerializer and the DataContractSerializer - http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

+1  A: 

Just a thought, but does the DataContractSerializer have the same problem?

Damian Powell
Great idea, I wonder if I can get the serialised content out as a string? Cheers, I'll give it a crack!
Kieron
DataContract intentionally gives you far less control over the serialization form than XmlSerializer does. It's only recommended when you care little about the shape of the XML and more about the shape of the class.
Josh Einstein
Worked perfectly, don't need to worry about the content now - it just serializes. Thanks!
Kieron