By default, .Net ends up xml serializing nullable types into a node that looks similar to this
<SomeNode p3:nil="true" xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" />
Unfortunately, given that the object model I am serializing has many many many null values, I end up with a very large xml document (185mb) when it should be much smaller with the null nodes removed completely (20mb)
The object definition is autogenerated when I add a webservice reference, so, thankfully it is declared as a partial class and I am able to create my own partial classes that add a bunch of ShouldSerialize*
methods to prevent serializing any null values.
However, this is rather tedious, as the classes are large, and there are many of them.
Is there a way I can use reflection, to simplify the process of adding ShouldSerialize*
methods to a class at runtime for all public properties?
Like I wrote, I did this manually for some of the classes, and it's highly repetitive, 100s of functions that all look like this
public bool ShouldSerializeNotes() { return Notes != null; }
Thanks, -c
Similar to Marc's idea, but a bit more streamlined, you could stick all the class and property names into a text or XML file, then use that as the input to a T4 template that generates the partial classes with the ShouldSerialize* methods. T4 seems to be completely undocumented in VS2008, but if you give a text file in your solution a .tt extension, it should set you up with the right Custom Tool for code generation. Everything is supported properly in VS2010.