I apologize for the length of this post, but this particular problem
takes a bit of explanation.
I have a set of hierarchically-related classes that I want to serialize
on demand at runtime. There are two scenarios for serialization: the
first is what I refer to as a "reduced" values set, where I will
serialize only a selected few properties from each class in order to
reduce the wire size of the xml.
The second scenario is a "full" values set, where all public properties
of each object in the hierarchy will be serialized as part of a runtime
configuration request. This would be used for configuration transfer,
backups, etc.
I know how to prevent properties with primitive data types from being
serialized, and I know that to prevent serialization of reference
types, you just set the property value to null. The problem is, if I
set certain runtime object properties to null, I'll break a lot of
application logic.
So, here are the various ideas that I've come up with so far for
dealing with this issue:
1. Create a "reduced" schema for the first scenario, generate the
types, and instantiate them at runtime strictly for serialization
purposes.
2. Create a second copy of the full hierarchical set of objects, and
set the appropriate property values to null at the time of
serialization.
3. Create a custom Xml serializer that serializes the appropriate
properties based on which scenario is chosen.
None of these approaches seems particularly desirable. The first two
are inefficient from the standpoint of runtime memory management, and
the second approach has the added disadvantage of having to create a
mechanism for keeping the two sets of objects in sync at all times,
both at runtime and at design time. And the third approach seems
redundant, considering that the XmlSerializer class is available to do
the job.
Can anyone suggest a better alternative?