By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Can you serialize nested objects to XML?

P: n/a
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;
...
}
Apr 17 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <By***@discussions.microsoft.comwrote in message
news:C1**********************************@microsof t.com...
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;
...
}

Apr 17 '07 #2

P: n/a
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" <un**********@nothinks.comwrote in message
news:uT**************@TK2MSFTNGP04.phx.gbl...
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" <By***@discussions.microsoft.comwrote in message
news:C1**********************************@microsof t.com...
>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;
...
}


Apr 17 '07 #3

P: n/a

Be careful on the nested objects, when they are collection based.

See:

9/21/2005
XmlSerialization with IDictionary and CollectionBase Objects
http://sholliday.spaces.live.com/blog/
I have several downloadable examples there also.


"Byron" <By***@discussions.microsoft.comwrote in message
news:C1**********************************@microsof t.com...
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;
...
}

Apr 17 '07 #4

P: n/a
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" <am****@hotmail.comwrote in message
news:eZ**************@TK2MSFTNGP06.phx.gbl...
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" <un**********@nothinks.comwrote in message
news:uT**************@TK2MSFTNGP04.phx.gbl...
>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" <By***@discussions.microsoft.comwrote in message
news:C1**********************************@microso ft.com...
>>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;
...
}



Apr 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.