Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

xmlserializer and value/reference types

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Webb - 19 Jan 2008 07:17 GMT
Following a suggestion in this newsgroup, I have used xmlserializer to
construct a save file. It works extremely well, and reduced a task that I
though would take a couple of days to less than an hour, learning time
included.

I did notice one strange thing though. It generated and loaded almost all of
my instance data, with the sole exception of the color fields. Looking at
the xml file, it generates a "stub" for color, but does not store the actual
color data. Indeed, the xml file consists entirely of value fields, with no
reference fields.

It took about 30 seconds to extract the RGB values and store them as value
fields in my class, and reconstitute the colors from these fields on load.

But it does seem strange that this wasn't done automatically for me - the
colors could have been saved in the xml as color.R, color.G, color.B and
rebuilt on load.

Why is this? Am I correct that xmlserializer will only work on value fields?
If so, why does this limitation exist? What am I missing?

Peter Webb
Marc Gravell - 19 Jan 2008 09:03 GMT
It doesn't relate to reference vs value types - and in fact, Color
*is* a value-type.
I don't know why it didn't serialize (although I have seen the
question before). An easy option would be to store the ARGB value as
an int, and use ToArgb and FromArgb to translate. Another option might
be to try using a serialization surrogate, but this is harder.
Finally, you could try DataContractSerializer - see if it behaves the
same.

Marc
Peter Ritchie [C# MVP] - 19 Jan 2008 16:32 GMT
As Marc says Color is a value type.

Reference types will serialize via XmlSerialize, in fact most of the demoes
on MSDN about XmlSerialize deal with classes (Reference types).  

Maybe if you post your code we can see if there's something that's not being
done correctly.

Signature

Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> Following a suggestion in this newsgroup, I have used xmlserializer to
> construct a save file. It works extremely well, and reduced a task that I
[quoted text clipped - 18 lines]
>
> Peter Webb
Peter Webb - 20 Jan 2008 12:11 GMT
Thanks to you both.

I looked at the C# list of value types - int, bool etc - and assumed they
were all there were. I didn't notice that many of the things I thought of as
.NET classes were actually structs. Doh. I had long wondered why things like
Color, Point, Rectangle etc were reference types. Now I know.

So I learned rather more than I expected.

Thanks again.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.