Groups | Blog | Home
all groups > dotnet xml > december 2005 >

dotnet xml : Serialization: Two Scenarios - Best Approach?


integragreg NO[at]SPAM yahoo.com
12/4/2005 5:13:34 AM
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?
Chris Lovett
12/4/2005 2:07:44 PM
How about you serialize the "full" set - then filter the XML output via XSL
transform to produce the "reduced" set?

[quoted text, click to view]

Truong Hong Thi
12/4/2005 8:26:11 PM
If you are using SoapFomatter, you can use ISerializationSurrogate. For
XmlSerializer, I don't know such equivalent option.
integragreg NO[at]SPAM yahoo.com
12/6/2005 3:03:19 AM
An excellent idea! I had not thought of that approach. I'll
investigate that further.

Thanks, Chris.
integragreg NO[at]SPAM yahoo.com
12/6/2005 3:09:49 AM
Troung,

In my application, the Xml could certainly be serialized using the
SoapFormatter. I'll check that out as well.

Thanks!
AddThis Social Bookmark Button