I'm trying to serialize an object with several properties, but I don't want to include all properties in the serialization. Also, I would like to change the date format.
Of course I could add [XmlIgnore]
, but I'm not allowed to change the original class.
The only option I could think of was to create a new class and copy all contents between the two classes. But that would be ugly and would require a lot of manual code.
Would it maybe be possible to maybe create a subclass, since the original is not abstract?
My question is thus:
How can I exclude some properties without changing the original class?
How can I customize the date format of the output XML?
Requirements:
As strong typed as possible
Serialized XML should be deserializable
Thanks in advance.
If you're using
XmlSerializer
,XmlAttributeOverrides
is probably what you need.Update: I've been investigating the possibilities of customizing the date format, and, as far as I can see, no pretty solutions exist.
One option, as has been mentioned by others, is to implement
IXmlSerializable
. This has the drawback that you're completely responsible for (de-)serializing the entire object(-graph).A second option, with also quite an extensive list of drawbacks, is to subclass the base class (you mentioned it as an alternative in your post). With quite some plumbing, conversions from and to the original object, and the use of
XmlAttributeOverrides
you could build something like this:It ain't pretty, but it will work.
Note that you're actually (de-)serializing SubTest objects in this case. If the exact type is important, this is not going to be an option too.
For whoever is interested, I decided to use
XmlAttributeOverrides
, but made them more strong typed (I hate to type property names as strings). Here is the extension method I used for it:Then, to ignore an attribute, I can beautifully add it to the ignore list:
You may be able to exclude some properties by taking advantage of the fact that the
XmlSerializer
will not serialize nulls to the output. So for reference types, you can null out those properties you don't want to appear in the xml.The resulting xml will be deserializable back into the same class, but the omitted fields will obviously be null.
However, this doesn't help for your desire to change the date format. For this, you'll need to either create a new class that has the date as a string in the format you want, or you could implement
IXmlSerializable
, giving you complete control over the xml. [it is worth noting that date data-type has a standard format in XML, so by changing it it won't stricly be an XML date any longer - you may not care].[EDIT in response to your comments]
There is an additional trick you might use to "disappear" a null nullable type, but it does require a change to your class. The serializer, when serializing
MyProperty
- will also check if there is a property calledMyProperySpecified
. If it exists and returns false, the item property is not serialized:If you are prepared to add this property you can make it remove the nullable types when null. In fact - now I think about it - this may be a useful way of removing other properties too, depending on your usage scenario.
First options is to use XmlAttributeOverrides class.
Or you can try to create derived class and implement IXmlSerializable interface