473,412 Members | 2,092 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,412 software developers and data experts.

Deserialize property w/ setter but no getter in 2.0???

Is it possible to deserialize a class that has a public property w/ a
setter, but no getter?

I'm not finding anything that would allow this --

Presuming that is is NOT possible, what are the best practices for
implementing versioning for changing properties?

Thanks
Oct 5 '06 #1
6 3483
If you have the source code, you can tag it with [XmlIgnore]

Oct 6 '06 #2
Keith Patrick wrote:
If you have the source code, you can tag it with [XmlIgnore]
Right, but that applies to the property.

Here is an example of what I want:

public class Data
{
string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

//...

XmlSerializer serializer = new XmlSerializer(typeof(Data));
XmlTextReader reader = new XmlTextReader("data.xml");
Data data = (Data)serializer.Deserialize(reader);
// Presume that data.xml is:

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StringValue>Peter</StringValue>
</Data>

----

With the above, after deserialization, data.stringValue will NOT contain
the value "Peter".

From what I've seen, the XmlSerializer will serialize/deserialize
properties that have both a getter and setter.

This seems to be a huge weakness to the serialization, as such a
requirement is purely arbitrary.

Anyone have any comments/suggestions/workarounds?
Oct 6 '06 #3
XmlSerializer serializes only public fields. Either you need to declare
stringValue as public memeber or have access to your private member via
public properties.

public class Data
{
public string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

This should work.

"Peter Franks" wrote:
Keith Patrick wrote:
If you have the source code, you can tag it with [XmlIgnore]

Right, but that applies to the property.

Here is an example of what I want:

public class Data
{
string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

//...

XmlSerializer serializer = new XmlSerializer(typeof(Data));
XmlTextReader reader = new XmlTextReader("data.xml");
Data data = (Data)serializer.Deserialize(reader);
// Presume that data.xml is:

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StringValue>Peter</StringValue>
</Data>

----

With the above, after deserialization, data.stringValue will NOT contain
the value "Peter".

From what I've seen, the XmlSerializer will serialize/deserialize
properties that have both a getter and setter.

This seems to be a huge weakness to the serialization, as such a
requirement is purely arbitrary.

Anyone have any comments/suggestions/workarounds?
Oct 9 '06 #4
Ujjwal Karjee wrote:
XmlSerializer serializes only public fields. Either you need to declare
stringValue as public memeber or have access to your private member via
public properties.

public class Data
{
public string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

This should work.
It may work, but it is forced specific (undesired) design constraints
because of an arbitrary and extremely poor design of the XmlSerializer
class.

Note to those that are considering using the framework serialization for
version-portable files -- DON'T!

>
"Peter Franks" wrote:

>>Keith Patrick wrote:
>>>If you have the source code, you can tag it with [XmlIgnore]

Right, but that applies to the property.

Here is an example of what I want:

public class Data
{
string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

//...

XmlSerializer serializer = new XmlSerializer(typeof(Data));
XmlTextReader reader = new XmlTextReader("data.xml");
Data data = (Data)serializer.Deserialize(reader);
// Presume that data.xml is:

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StringValue>Peter</StringValue>
</Data>

----

With the above, after deserialization, data.stringValue will NOT contain
the value "Peter".

From what I've seen, the XmlSerializer will serialize/deserialize
properties that have both a getter and setter.

This seems to be a huge weakness to the serialization, as such a
requirement is purely arbitrary.

Anyone have any comments/suggestions/workarounds?
Oct 9 '06 #5
I don't know how poorly designed XmlSerializer is. We have an option to
bypass their desing by implementing IXmlSerializable Interface. In this case
you can serialize what you want and how you want. I like IXmlSerializable
interface and find it very useful.

"Peter Franks" wrote:
Ujjwal Karjee wrote:
XmlSerializer serializes only public fields. Either you need to declare
stringValue as public memeber or have access to your private member via
public properties.

public class Data
{
public string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

This should work.

It may work, but it is forced specific (undesired) design constraints
because of an arbitrary and extremely poor design of the XmlSerializer
class.

Note to those that are considering using the framework serialization for
version-portable files -- DON'T!


"Peter Franks" wrote:

>Keith Patrick wrote:

If you have the source code, you can tag it with [XmlIgnore]

Right, but that applies to the property.

Here is an example of what I want:

public class Data
{
string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

//...

XmlSerializer serializer = new XmlSerializer(typeof(Data));
XmlTextReader reader = new XmlTextReader("data.xml");
Data data = (Data)serializer.Deserialize(reader);
// Presume that data.xml is:

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StringValue>Peter</StringValue>
</Data>

----

With the above, after deserialization, data.stringValue will NOT contain
the value "Peter".

From what I've seen, the XmlSerializer will serialize/deserialize
properties that have both a getter and setter.

This seems to be a huge weakness to the serialization, as such a
requirement is purely arbitrary.

Anyone have any comments/suggestions/workarounds?
Oct 9 '06 #6
Ujjwal Karjee wrote:
I don't know how poorly designed XmlSerializer is. We have an option to
bypass their desing by implementing IXmlSerializable Interface. In this case
you can serialize what you want and how you want. I like IXmlSerializable
interface and find it very useful.
Ok, I'll look into that interface a little more.

However, it seems to be overkill for what amounts to a problem due to
what appears to be an arbitrary design failure of the XmlSerializer in
that it won't serialize a public property unless it has both a getter
and a setter.

>
"Peter Franks" wrote:

>>Ujjwal Karjee wrote:
>>>XmlSerializer serializes only public fields. Either you need to declare
stringValue as public memeber or have access to your private member via
public properties.

public class Data
{
public string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

This should work.

It may work, but it is forced specific (undesired) design constraints
because of an arbitrary and extremely poor design of the XmlSerializer
class.

Note to those that are considering using the framework serialization for
version-portable files -- DON'T!
>>>

"Peter Franks" wrote:

Keith Patrick wrote:
>If you have the source code, you can tag it with [XmlIgnore]

Right, but that applies to the property.

Here is an example of what I want:

public class Data
{
string stringValue;
public string StringValue
{
set { stringValue = value; }
}
}

//...

XmlSerializer serializer = new XmlSerializer(typeof(Data));
XmlTextReader reader = new XmlTextReader("data.xml");
Data data = (Data)serializer.Deserialize(reader);
// Presume that data.xml is:

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StringValue>Peter</StringValue>
</Data>

----

With the above, after deserialization, data.stringValue will NOT contain
the value "Peter".

From what I've seen, the XmlSerializer will serialize/deserialize
properties that have both a getter and setter.

This seems to be a huge weakness to the serialization, as such a
requirement is purely arbitrary.

Anyone have any comments/suggestions/workarounds?
Oct 10 '06 #7

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

Similar topics

3
by: kepes.krisztian | last post by:
Hi ! I want to create a property that can use parameter(s). In Delphi I can create same thing (exm: Canvas.Pixel -> Canvas.GetPixel(self,X,Y):integer; Canvas.SetPixel(self,X,Y,Color::integer); ...
4
by: Jimbo | last post by:
I am sort of new to C#. Currently have a private property called "_name" in a class. I have written a public getter and setter routine for it called "Name". Currently, the getter for the...
13
by: Peter Kirk | last post by:
Hi there, can someone tell me what exactly a "property" is in a C# class? As far as I can see it is "two methods" - ie a getter and a setter for an instance variable. What is the difference...
5
by: Brent Ritchie | last post by:
Hello, This is my first attempt at template programming so please bear with me. This is what I have so far: Property.h -------------------------- #ifndef PROPERTY_H_ #define PROPERTY_H_
7
by: none | last post by:
I'm trying to implement a simple repeateable property mechansism so I don't have to write accessors for every single instance variable I have. ------------ classMyObject: def __init__ (self):...
2
by: Ben Voigt | last post by:
The C# Language Specification says: A virtual property declaration specifies that the accessors of the property are virtual. The virtual modifier applies to both accessors of a read-write...
5
by: Paul Richardson | last post by:
Hi, What i'm trying to do is assign a class (i.e. UKPostCode) to a propety of the UKAddress Class. So for example: class UKAddress { public string AddressLine1
0
by: Gabriel Genellina | last post by:
En Fri, 02 May 2008 18:29:57 -0300, Alex Fainshtein <alex_fainshtein@netzero.netescribió: property requires a new-style class; change the first line to class Test(object):...
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
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?
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
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: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.