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

Serializing properties

Hi Folks!

Here's a strange behaviour:

Without a properties SET accessor (see code below), the property will not
serialize.

public class myObject
{

private string _myAttribute;

[XmlAttribute("MyAttrib")]
public string myAttribute
{
get { return _myAttribute; }
set { _myAttribute = value; } //this accessor must be present to
serialize
}

public myObject()
{
_myAttribute="set during construction...";
}

}

I would prefer the property (myAttribute) be accessible only by GET but if I
want it to serialize I must allow the SET. Is there any way around this?

Rein
Nov 15 '05 #1
6 3369
Rein,

This makes sense. XML serialization uses the accesor to the property to
set the value, it does not access the fields on the class level. If you
want to get around this, use the SoapFormatter and use regular
serialization.

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

"Rein Petersen" <rm********@bogus.hotmail.com> wrote in message
news:uA**************@TK2MSFTNGP11.phx.gbl...
Hi Folks!

Here's a strange behaviour:

Without a properties SET accessor (see code below), the property will not
serialize.

public class myObject
{

private string _myAttribute;

[XmlAttribute("MyAttrib")]
public string myAttribute
{
get { return _myAttribute; }
set { _myAttribute = value; } //this accessor must be present to
serialize
}

public myObject()
{
_myAttribute="set during construction...";
}

}

I would prefer the property (myAttribute) be accessible only by GET but if I want it to serialize I must allow the SET. Is there any way around this?

Rein

Nov 15 '05 #2
Rein,

You need the set accessor if you are serialising, as you need to provide a
way to de-serialise.

You can apply the "ReadOnlyAttribute" to the property, which prevents the
user from changing the property at design time.

You could probably also have your serialized property completely hidden
(apply the Browsable, and EditorBrowsable attributes), and have another
property with just the Get which is not serialized.

Tim.

"Rein Petersen" <rm********@bogus.hotmail.com> wrote in message
news:uA**************@TK2MSFTNGP11.phx.gbl...
Hi Folks!

Here's a strange behaviour:

Without a properties SET accessor (see code below), the property will not
serialize.

public class myObject
{

private string _myAttribute;

[XmlAttribute("MyAttrib")]
public string myAttribute
{
get { return _myAttribute; }
set { _myAttribute = value; } //this accessor must be present to
serialize
}

public myObject()
{
_myAttribute="set during construction...";
}

}

I would prefer the property (myAttribute) be accessible only by GET but if I want it to serialize I must allow the SET. Is there any way around this?

Rein

Nov 15 '05 #3
> This makes sense. XML serialization uses the accesor to the property
to
set the value, it does not access the fields on the class level. If you
want to get around this, use the SoapFormatter and use regular
serialization.


Hmmm, I can't say I agree it makes sense - I'm not asking the serialization
process to SET the property, but rather to GET it and serialize it.

Is this really a sensible behaviour? Can anyone explain why this is?

Admittedly, I'm not keen on the SoapFormatter because I think SOAP sucks and
I doubt that I will be able to format the resulting serialized xml as I
require. Are there any decent resources detailing customizing the
serializing using the SoapFormatter where I can confirm this?

Rein
Nov 15 '05 #4
Thanks Tim,

Your explanation and suggestions solved my problem.

Rein

"Tim Johnson" <ti******@cae.ca> wrote in message
news:bn**********@dns3.cae.ca...
Rein,

You need the set accessor if you are serialising, as you need to provide a
way to de-serialise.

You can apply the "ReadOnlyAttribute" to the property, which prevents the
user from changing the property at design time.

You could probably also have your serialized property completely hidden
(apply the Browsable, and EditorBrowsable attributes), and have another
property with just the Get which is not serialized.

Tim.

"Rein Petersen" <rm********@bogus.hotmail.com> wrote in message
news:uA**************@TK2MSFTNGP11.phx.gbl...
Hi Folks!

Here's a strange behaviour:

Without a properties SET accessor (see code below), the property will not serialize.

public class myObject
{

private string _myAttribute;

[XmlAttribute("MyAttrib")]
public string myAttribute
{
get { return _myAttribute; }
set { _myAttribute = value; } //this accessor must be present to
serialize
}

public myObject()
{
_myAttribute="set during construction...";
}

}

I would prefer the property (myAttribute) be accessible only by GET but
if I
want it to serialize I must allow the SET. Is there any way around this?

Rein


Nov 15 '05 #5
Rein,

It makes sense because the operation has to go two ways. If you are
able to serialize a value, then you need to be able to read the value from
the object. If you want to de-serialize an value then you need to be able
to write the value to the object. Since the XML Serializer handles both
operations, it needs to know that whatever it can read from, it can also
write to. Granted, the XML serializer could have been coded to ignore
elements that don't have a representation in the object model (and vice
versa), but I think that they wanted to get some sort of type-safety in
there.

The SoapFormatter is going to be different, in the sense that your
properties are not going to be serialized. Rather, your internal fields on
your class are going to be serialized. Now if you have a basic one-to-one
mapping between your fields and your properties, then this is ok. However,
if your properties are a composite of many values in the fields, then you
probably don't want to duplicate the business logic to calculate those
fields. In this case, a better approach would be to have a separate object
which has public read only fields which you can set through the constructor
of the object. Your object would create an instance of this object, setting
the values. Then, it would serialize that using the SoapFormatter. Your
XML will then be easier to manipulate.

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

"Rein Petersen" <rm********@bogus.hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
This makes sense. XML serialization uses the accesor to the
property to
set the value, it does not access the fields on the class level. If you
want to get around this, use the SoapFormatter and use regular
serialization.
Hmmm, I can't say I agree it makes sense - I'm not asking the

serialization process to SET the property, but rather to GET it and serialize it.

Is this really a sensible behaviour? Can anyone explain why this is?

Admittedly, I'm not keen on the SoapFormatter because I think SOAP sucks and I doubt that I will be able to format the resulting serialized xml as I
require. Are there any decent resources detailing customizing the
serializing using the SoapFormatter where I can confirm this?

Rein

Nov 15 '05 #6
Nicholas,

Thanks for the insightful distinctions between Xml Serializer and
SoapFormatter. I now understand how your suggestion to use the SoapFormatter
(ableit for an unconventional purpose), can provide flexibility in
conforming serialization to a desired schema.

If the goal of serialization is to represent a snapshot of an object's
state, then serializing it's private fields (over public properties) makes
sense.

I'm certain this is my solution.

Thanks again!

Rein
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eS*************@TK2MSFTNGP11.phx.gbl...
Rein,

It makes sense because the operation has to go two ways. If you are
able to serialize a value, then you need to be able to read the value from
the object. If you want to de-serialize an value then you need to be able
to write the value to the object. Since the XML Serializer handles both
operations, it needs to know that whatever it can read from, it can also
write to. Granted, the XML serializer could have been coded to ignore
elements that don't have a representation in the object model (and vice
versa), but I think that they wanted to get some sort of type-safety in
there.

The SoapFormatter is going to be different, in the sense that your
properties are not going to be serialized. Rather, your internal fields on your class are going to be serialized. Now if you have a basic one-to-one
mapping between your fields and your properties, then this is ok. However, if your properties are a composite of many values in the fields, then you
probably don't want to duplicate the business logic to calculate those
fields. In this case, a better approach would be to have a separate object which has public read only fields which you can set through the constructor of the object. Your object would create an instance of this object, setting the values. Then, it would serialize that using the SoapFormatter. Your
XML will then be easier to manipulate.

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

"Rein Petersen" <rm********@bogus.hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
This makes sense. XML serialization uses the accesor to the

property
to
set the value, it does not access the fields on the class level. If you want to get around this, use the SoapFormatter and use regular
serialization.


Hmmm, I can't say I agree it makes sense - I'm not asking the

serialization
process to SET the property, but rather to GET it and serialize it.

Is this really a sensible behaviour? Can anyone explain why this is?

Admittedly, I'm not keen on the SoapFormatter because I think SOAP sucks

and
I doubt that I will be able to format the resulting serialized xml as I
require. Are there any decent resources detailing customizing the
serializing using the SoapFormatter where I can confirm this?

Rein


Nov 15 '05 #7

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

Similar topics

2
by: Jakob Bengtsson | last post by:
Hi, I have a form (which cannot be serialized). In the form's code I declare an object like this (never mind the object nor class name, it's for illustration only): Private WithEvents...
4
by: Angelos Karantzalis | last post by:
Hi guys. I've come across a problem when I tried to serialize a class into xml, only to discover that the parent class's XML Serialization properties weren't included in the output xml. ...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
1
by: Derrick | last post by:
Hello all; I seem to be having some trouble serializing a class to XML. This code is a cut & paste from a project which used it perfectly, but all of a sudden I'm getting an error that the "dll...
8
by: Joe | last post by:
Hello All: Say I have a solution with two projects (Project1 and Project2) and each project contains a class (Project1 contains Class1 and Project2 contains Class2). The projects don't...
3
by: axr | last post by:
Having trouble with Serilization of objects that contain members which are of type Interface eg public class SomeClass { ISomeInterface1 itf1; ClassType1 ct1; ISomeInterface2 itf2;
2
by: she_prog | last post by:
I have a class derived from UserControl. I need to serialize an object of this class, but only some properties of it, as not all properties are serializable (some of the properties coming from...
3
by: Jeremy | last post by:
I've created a serializable class and put attributes around all the properties that should be serialized. I return the class from a web service, but my problem is that the wsdl for the web service...
1
by: Karthik1979 | last post by:
I have a custom class inherited from List<T> collection. Along with the base class functionality, I have included my additional properties. When serializing, only the base class items are serialized...
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
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?
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...
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...

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.