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

XmlSerializer and DefaultValue?

Hello,

I'm currently playing around with the XmlSerializer to replace my old
handmade Xml parsing code, but am having a few small issues with
default values and derived classes.

Say we have classes A, B and C:

abstract class A
{
protected int a_value = 1;

[XmlAttribute("value"), DefaultValue(1)]
public int AValue
{
get { return a_value; }
set { a_value = value; }
}
}

class B : A
{
....
}

class C : A
{
....
}
Now, I want class B and C to have different default values, more
precisely, I want B to have 1 as default value and C to have
Int32.MaxValue. But I can't figure out how to actually make this work.
I tried making the property in A abstract and provide the
implementation in B and C with different default values, but it still
used the default value from A. I tried removing the default value from
A, which gave the result of having no default value, everything was
printed, always.

I then tried to remove the XmlAttribute attribute from A and only have
it and the default value in B and C, but it gave me the following
exception:

System.InvalidOperationException: There was an error reflecting
property 'AValue'. ---> System.InvalidOperationException: Member
B.AValue' hides inherited member 'A.AValue', but has different custom
attributes.
Anyone have any ideas for this? :-)

Thanks,
- Rasmus.

Apr 14 '06 #1
2 4122
Amy
Rasmus,

Did you resolve this issue? I just encountered the same problem and
have been searching for a solution, but yours is the only posting I've
found related to the XmlSerializer and default values in derived
classes.

An interesting side note is that the PropertyGrid control recognizes
that the value assigned to the derived class is the default value
specified for the overridden property.

Thanks!
Amy

Rasmus Kromann-Larsen wrote:
Hello,

I'm currently playing around with the XmlSerializer to replace my old
handmade Xml parsing code, but am having a few small issues with
default values and derived classes.

Say we have classes A, B and C:

abstract class A
{
protected int a_value = 1;

[XmlAttribute("value"), DefaultValue(1)]
public int AValue
{
get { return a_value; }
set { a_value = value; }
}
}

class B : A
{
...
}

class C : A
{
...
}
Now, I want class B and C to have different default values, more
precisely, I want B to have 1 as default value and C to have
Int32.MaxValue. But I can't figure out how to actually make this work.
I tried making the property in A abstract and provide the
implementation in B and C with different default values, but it still
used the default value from A. I tried removing the default value from
A, which gave the result of having no default value, everything was
printed, always.

I then tried to remove the XmlAttribute attribute from A and only have
it and the default value in B and C, but it gave me the following
exception:

System.InvalidOperationException: There was an error reflecting
property 'AValue'. ---> System.InvalidOperationException: Member
B.AValue' hides inherited member 'A.AValue', but has different custom
attributes.
Anyone have any ideas for this? :-)

Thanks,
- Rasmus.


May 4 '06 #2
No,

unfortunatly I havn't had a chance to work on that project since then,
so I havn't resolved my error yet. :-)

- Rasmus.

May 24 '06 #3

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

Similar topics

14
by: Maurice Mertens | last post by:
Hi, I have an Access database in which I defined default values for certain columns. On my windows forms I've got controls bound to the dataset, The problem is that when I add a new row to the...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
1
by: Mark | last post by:
How do I write the following code to set the default value? Me!xNum.DefaultValue = Me!XCurrency.DefaultValue = Me!XDate.DefaultValue = Me!XString.DefaultValue = Can I just put all the lines...
10
by: tony | last post by:
Hello!! I have some demo programs written in C# and they have this construction "" see below. I haven't seen this before so what does it mean ? public bool ShowDropDownButtons { get {...
4
by: SteveT | last post by:
I have a boolean property that I want to serialize to disk using XmlSerializer. I noticed that if the property includes the attribute "DefaultValue(true)" or "DefaultValue(false)" that the...
6
by: Peter Hartlén | last post by:
Hi! What's the correct syntax for the default value design time attribute, using classdiagram view and Custom Attributes dialog. For a boolean: DefaultValue(true) DefaultValue("true")...
7
by: kumar.senthil | last post by:
Hi, I'm using XmlSerializer to create an object from the XML string. I would like to know whether I can get a null value for an empty XML element. Actually the XmlSerializer assigns "" (empty...
5
by: Phil Galey | last post by:
I'm using XMLSerializer to serialize hierarchical Class-based objects and their content to XML. There are a number of fields (i.e. properties) that are optional, meaning that if no data id written...
2
by: dmonder | last post by:
I am using the XMLSerializer to map the XML below to an object as follows: Config config XmlSerializer xmls = new XmlSerializer( typeof( Config ) ); try { TextReader tr = new StreamReader(...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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.