Reasoning behind making XmlSerializer so “restrictive”?

  • Thread starter Thread starter klem s
  • Start date Start date
K

klem s

hi

1) Since B doesn’t contain any members of type E, when XmlSerializer
serializes object of type B to an xml file, it doesn’t create
subelement describing type E. It also doesn’t create subelement for
type A, from which B instance derives.

Any particular reason why it would be a bad idea for xmlFormat to also
create subelements for types E and A?



XmlSerializer xmlFormat = new XmlSerializer ( typeof( B ),
new Type[] { typeof( A ), typeof( D ),
typeof( E ) } );
class A{}
class B:A { D d = new D(): }
class D{}
class E{}



2)
a) Is there a particular reason why XmlSerializer only persists public
fields, but not private fields?

b) Why can’t XmlSerializer serialize types that don’t implement the
default constructor?

c) Why doesn’t XmlSerializer require classes to be marked with
serialized attribute.

thank you
 
You specify the types in the array so that XmlSerializer knows what to
compile at run-time to support serialization. That is, my recollection
is that XmlSerializer is generating code on the fly to handle the
implementation for a given type. The array allows you to control what
kind of code is generated, not how the XML is formatted.
Uhm, I’m not sure I understand this. I know I’m simplifying things
here, but … what’s the point of generating that particular part of
code ( caused by array containg element typeof( E ) ), if this code
won’t then be used for creating xml subelement E?

BTW - Even if I don’t specify typeof( D ) as the second argument for
XmlSerializer constructor, XmlSerializer will still create subelement
D. Thus, xml output is exactly the same with either of the following
two constructor calls:

XmlSerializer xmlFormat = new XmlSerializer ( typeof( B ),
new Type[] { typeof( A ), typeof( D ) } );

XmlSerializer xmlFormat = new XmlSerializer ( typeof( B ),
new Type[] { typeof( A ) } );

class A{}
class B:A { D d = new D(): }
class D{}

What’s the point of specifying type information( via typeof ) for
subelements, if they get created regardless?


thank you
 
Back
Top