Rein,
It makes sense because the operation has to go two ways. If you are
able to serialize a value, then you need to be able to read the value from
the object. If you want to de-serialize an value then you need to be able
to write the value to the object. Since the XML Serializer handles both
operations, it needs to know that whatever it can read from, it can also
write to. Granted, the XML serializer could have been coded to ignore
elements that don't have a representation in the object model (and vice
versa), but I think that they wanted to get some sort of type-safety in
there.
The SoapFormatter is going to be different, in the sense that your
properties are not going to be serialized. Rather, your internal fields on
your class are going to be serialized. Now if you have a basic one-to-one
mapping between your fields and your properties, then this is ok. However,
if your properties are a composite of many values in the fields, then you
probably don't want to duplicate the business logic to calculate those
fields. In this case, a better approach would be to have a separate object
which has public read only fields which you can set through the constructor
of the object. Your object would create an instance of this object, setting
the values. Then, it would serialize that using the SoapFormatter. Your
XML will then be easier to manipulate.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Rein Petersen" <rm********@bogus.hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
This makes sense. XML serialization uses the accesor to the
property to set the value, it does not access the fields on the class level. If you
want to get around this, use the SoapFormatter and use regular
serialization.
Hmmm, I can't say I agree it makes sense - I'm not asking the
serialization process to SET the property, but rather to GET it and serialize it.
Is this really a sensible behaviour? Can anyone explain why this is?
Admittedly, I'm not keen on the SoapFormatter because I think SOAP sucks
and I doubt that I will be able to format the resulting serialized xml as I
require. Are there any decent resources detailing customizing the
serializing using the SoapFormatter where I can confirm this?
Rein