开发者

Handling how .Net Serializes Nullable types

By default, .Net ends up xml serializ开发者_StackOverflow中文版ing 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.


As a 1-off you could just write the property names to a text file (via type.GetProperties()), and use basic tools (a spreadsheet would suffice) to generate a basic method be property. Not sophisticated, but pragmatic. Of course you'd need to account for ongoing changes.

To answer the question as posed; no, not really (unless you implement IXmlSerializable, which is a pain).

I would ask, though: does it have to be XML? I could make that much smaller and have the behaviour you describe (no data written for nulls) by changing data format. That might not be an option, but that does seem unnecessarily large.


My solution: add the attribute to the root node of the document like this:

var writer = XmlWriter.Create(...);

writer.WriteStartElement("root");
writer.WriteAttributeString("xmlns", "p3", null, @"http://www.w3.org/2001/XMLSchema-instance");

Then XmlSerializer doesn't have to write out the namespace at every nullable element.


In my case the nullable variables/elements were all String type. So, I simply performed a check and assigned them string.Empty in case of NULL. This way I got rid of the unnecessary nil and xmlns attributes (p3:nil="true" xmlns:p3="http://www.w3.org/2001/XMLSchema-instance)

// Example:

myNullableStringElement = varCarryingValue ?? string.Empty

// OR

myNullableStringElement = myNullableStringElement ?? string.Empty
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜