views:

89

answers:

2

What ways there are for doing a one way XML serialization in .Net? XmlSerializer won't write properties that don't have a public setter. While this limitation is understandable for deserialization I would be satisfied with just serialization for web use. The JSON.Net serialization works great in this situation as it serializes the full object graph not minding even if some properties have no setters defined.

The main goal for these classes is a normal .Net class library. These classes are consumed by other code in 90% of the cases so I am after a solution that doesn't sacrifice the normal usability of the classes.

The resulting class library should support .Net 2.0.

+3  A: 

I would take a look at the DataContractSerializer. While this type is declared in the .NET 3.0 framework that would not stop you from using the new version of System.Runtime.Serialization.dll in your project.

For a good understanding of how DataContractSerializer differs from XmlSerializer I would check out Chris Lively's answer.

Andrew Hare
Isnt' the DataContractSerializer part of WCF and thus only available with .NET 3.0 and higher?? THe OP specifically asked for .NET 2.0 .....
marc_s
@marc_s - You are correct, DataContractSerializer is part of .NET 3.0 but since 2.0 and 3.x share the same CLR (2.0) you are free to use 3.x assemblies in a 2.0 application.
Andrew Hare
Yes - but you'd still have to install the .NET 3.0 framework, right? Not that I think it would be a big deal - but a lot of sysadmins do, unfortunately :-(
marc_s
Not necessarily, you could install the framework somewhere and then copy the assembly from that location an make it a part of your project. You wouldn't have to install the new version of the framework on production boxes.
Andrew Hare
I don't think I want to ship _part_ of the 3.0 framework as a part of the class library to the possible clients. Especially since the System.Runtime.Serialization.dll seems to have a dependency to SMDiagnostics which is some internal library of Microsoft I guess.
Mikko Rantanen
+2  A: 

You might want to check this site out: XmlSerializer vs DataContractSerializer

Beyond that you might consider overriding how your object is being serialized and deciding for yourself what should and should not be in the xml stream.

Chris Lively
You could implement the ISerializable interface.
eschneider