Can you serialize nested objects to XML?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I try to serialize an instance of the LocationCell below (note Building
field) I get an error in the reflection attempt. If I remove the _Building
field it serializes fine. I tried renaming Building._Name to Building._BName
in case the duplicate name was the issue, but that didn't help.

Is there a native way to serialize nested objects, or will I have to write
my own?

[Serializable]
public class LocationCell
{
private Building _Building;
private string _Name = String.Empty;
...
}

[Serializable]
public class Building
{
private string _Name = String.Empty;
...
}
 
XML Serialization requires that a class must have a parameterless
constructor.

In addition, any class members that are not public are not serialized.
Methods are not serialized. And properties that do not have both a get and a
set accessor are not serialized.

Serializing nested objects is not a problem. Everything in .Net is an
object.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Kevin,

I don't understand what you mean about the properties. Why would you want
to serialize properties, wouldn't the state data be serialized? So when you
deserialize the object the properties simply read the state data.

Also, for private members, couldn't you provide a custom serialize method to
nest deep, kinda like a deep clone?

Just curious.


Kevin Spencer said:
XML Serialization requires that a class must have a parameterless
constructor.

In addition, any class members that are not public are not serialized.
Methods are not serialized. And properties that do not have both a get and
a set accessor are not serialized.

Serializing nested objects is not a problem. Everything in .Net is an
object.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Byron said:
When I try to serialize an instance of the LocationCell below (note
Building
field) I get an error in the reflection attempt. If I remove the
_Building
field it serializes fine. I tried renaming Building._Name to
Building._BName
in case the duplicate name was the issue, but that didn't help.

Is there a native way to serialize nested objects, or will I have to
write
my own?

[Serializable]
public class LocationCell
{
private Building _Building;
private string _Name = String.Empty;
...
}

[Serializable]
public class Building
{
private string _Name = String.Empty;
...
}
 
I don't understand what you mean about the properties. Why would you want
to serialize properties, wouldn't the state data be serialized? So when
you deserialize the object the properties simply read the state data.

You can serialize public fields, but to serialize properties there must be a
public get and set method, because the serializer must be able to read the
property to serialize it, and to write the property in order to deserialize
it. All members must be public for the same reason. The serializer must be
able to see any member it serializes/deserializes.
Also, for private members, couldn't you provide a custom serialize method
to nest deep, kinda like a deep clone?

You can certainly write a custom serializer, and do anything you want. But
in order to get access to private properties, you would have to serialize
the binary representation of the class in memory, again, because private
members are not exposed.

I believe the XmlSerializer class was designed the way it was for
efficiency/speed. Methods are automatically created when you create an
instance of a class, so the serializer doesn't actually build the class, but
calls its' parameterless constructor, and then populates it by assigning all
publicly-available members from the XML and reflection.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

AMDRIT said:
Kevin,

I don't understand what you mean about the properties. Why would you want
to serialize properties, wouldn't the state data be serialized? So when
you deserialize the object the properties simply read the state data.

Also, for private members, couldn't you provide a custom serialize method
to nest deep, kinda like a deep clone?

Just curious.


Kevin Spencer said:
XML Serialization requires that a class must have a parameterless
constructor.

In addition, any class members that are not public are not serialized.
Methods are not serialized. And properties that do not have both a get
and a set accessor are not serialized.

Serializing nested objects is not a problem. Everything in .Net is an
object.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Byron said:
When I try to serialize an instance of the LocationCell below (note
Building
field) I get an error in the reflection attempt. If I remove the
_Building
field it serializes fine. I tried renaming Building._Name to
Building._BName
in case the duplicate name was the issue, but that didn't help.

Is there a native way to serialize nested objects, or will I have to
write
my own?

[Serializable]
public class LocationCell
{
private Building _Building;
private string _Name = String.Empty;
...
}

[Serializable]
public class Building
{
private string _Name = String.Empty;
...
}
 
Back
Top