473,416 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Can you serialize nested objects to XML?

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
4 7062
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
2
by: Dave | last post by:
I have an application running on a 3 server webfarm running Windows 2003 with SQLServer Session state. After running for several hours, I started getting the following error. When I access each...
1
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. ...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
1
by: Rick Luckwell | last post by:
I have 3 collections(reports, services, charts) of objects(report, service, chart) that are nested with each other. When I serialize the object the output only contains reports and services but...
4
by: teddysnips | last post by:
I would like to serialize a class to XML. The format of the XML is fixed (see below). It only needs to be serialized - it is read by another application. I know I can do it manually using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.