473,385 Members | 1,218 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,385 software developers and data experts.

Object serialization with read-only members

Hi Group,

I've defined a class with an 'ID' property, which is a GUID that is created
at the time of the very first object instance creation. This member is not
included when I serialize an object instance to XML, since it is read-only.
Actually, I want to preserve the ID value with serialization/deserialization
processes but I do *not* want it to be writeable from code by a simple
myObject.ID = ... assignment - how to achieve this?

As far as I can see, I have to write my own serialization/deserialization
routines for this purpose. Does anyone know a less laborious way to get what
I want here, e.g. by a certain ["AttributeLabel"] for XML serialization that
I don't know yet?

Thanks for your help,
greetings

Hans
Jul 20 '06 #1
5 2513
"Hans-J?rgen Philippi" <HJ********@aol.comwrote:
Hi Group,

I've defined a class with an 'ID' property, which is a GUID that is created
at the time of the very first object instance creation. This member is not
included when I serialize an object instance to XML, since it is read-only.
Actually, I want to preserve the ID value with serialization/deserialization
processes but I do *not* want it to be writeable from code by a simple
myObject.ID = ... assignment - how to achieve this?

As far as I can see, I have to write my own serialization/deserialization
routines for this purpose. Does anyone know a less laborious way to get what
I want here, e.g. by a certain ["AttributeLabel"] for XML serialization that
I don't know yet?
There needs to be a "setter" on there because when the class is again
deserialized, it has to set the value back to the object. XML serialization
does not work the same as the native .NET serialization, as it goes against
properties rather than instance variables. I find this rather annoying as
well. In short, I believe you need to roll your own.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Jul 20 '06 #2
Hans,

Assuming that the field internally is not read only (just the property
is), you could achieve what you want by implementing IXmlSerializable. The
XmlSerializer will call this interface to serialize and deserialize your
class. Basically, you would output the id tag, and then set it internally
when your instance is dehydrated.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Hans-Jürgen Philippi" <HJ********@aol.comwrote in message
news:uk**************@TK2MSFTNGP05.phx.gbl...
Hi Group,

I've defined a class with an 'ID' property, which is a GUID that is
created at the time of the very first object instance creation. This
member is not included when I serialize an object instance to XML, since
it is read-only.
Actually, I want to preserve the ID value with
serialization/deserialization processes but I do *not* want it to be
writeable from code by a simple myObject.ID = ... assignment - how to
achieve this?

As far as I can see, I have to write my own serialization/deserialization
routines for this purpose. Does anyone know a less laborious way to get
what I want here, e.g. by a certain ["AttributeLabel"] for XML
serialization that I don't know yet?

Thanks for your help,
greetings

Hans


Jul 20 '06 #3
Thomas,

Not really, you can always implement IXmlSerializable and then you can
put any values you want in the output stream, as well as read them back in
when you rehydrate your object.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Thomas T. Veldhouse" <ve*****@yahoo.comwrote in message
news:3P******************************@giganews.com ...
"Hans-J?rgen Philippi" <HJ********@aol.comwrote:
>Hi Group,

I've defined a class with an 'ID' property, which is a GUID that is
created
at the time of the very first object instance creation. This member is
not
included when I serialize an object instance to XML, since it is
read-only.
Actually, I want to preserve the ID value with
serialization/deserialization
processes but I do *not* want it to be writeable from code by a simple
myObject.ID = ... assignment - how to achieve this?

As far as I can see, I have to write my own serialization/deserialization
routines for this purpose. Does anyone know a less laborious way to get
what
I want here, e.g. by a certain ["AttributeLabel"] for XML serialization
that
I don't know yet?

There needs to be a "setter" on there because when the class is again
deserialized, it has to set the value back to the object. XML
serialization
does not work the same as the native .NET serialization, as it goes
against
properties rather than instance variables. I find this rather annoying as
well. In short, I believe you need to roll your own.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Jul 20 '06 #4
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
Thomas,

Not really, you can always implement IXmlSerializable and then you can
put any values you want in the output stream, as well as read them back in
when you rehydrate your object.
Oh, yes indeed. I am not sure why that didn't occur to me. One thing I can
say is that it is very tedious to do that [and ISerializable] if you have a
large object graph.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Jul 20 '06 #5
Hi Nicholas,
Assuming that the field internally is not read only (just the property
is), you could achieve what you want by implementing IXmlSerializable.
The XmlSerializer will call this interface to serialize and deserialize
your class. Basically, you would output the id tag, and then set it
internally when your instance is dehydrated.
Yes, you're right, the private ID string is writeable without having a set
{...} part in its related public property code.
The point is: If I implement the IXmlSerializable interface, I end up
writing the complete serialization/deserialization code on my own, right?
And this is what I didn't want in my large class with a lot of members...

Maybe there's a way to interact with the intrinsic XML serialization for a
certain field only? That's what I was looking for.

Thanks anyway,
Hans
Jul 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andreas Palsgård | last post by:
Hey.. i want to write a class object containing both strings and integers, to a file on disk, and be able to read it properly again at another time. I made the code below... it doesn't work, but...
0
by: Philip Reed | last post by:
I'm trying to write a preferences-handling infrastructure that serializes prefs to XML. Basically I want to read in a common "default" prefs set, then read in the user's prefs and override the...
1
by: Philip K | last post by:
I was trying to use Soap serialisation to save some systems settings how ever it fails if the settings contain a Font object, works fine if the Font object is not in the object being serialised. ...
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 {
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
4
by: DazedAndConfused | last post by:
I encryted a serialized binary formatted object. Now I can't figure out how to deserialize it so that I can decrypt it. I used this code encrypt and write it out: Dim fe As New...
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
2
by: Thomas S | last post by:
Any suggestions on how to deserialize an object from one line of XML? I'm trying to deserialize multiple objects from one XML document, each object on one line of the file. The serialization is...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
2
by: David Thielen | last post by:
Hi; I have a small XML file that I need to read/change from my app. Is there some easy way to map from XML to my objects so I can just read it in to my objects, change the objects as needed,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.