469,889 Members | 1,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

XmlSerialization, inheritance and attribute overriding.

Ian
I've got this problem with overriding attributes on an inheritance
chain where the XmlSerializer is concerned. For example:

public class A
{
private string aWord = String.Empty();

[System.Xml.Serialization.XmlElementAttribute("a-word")]
public virtual string Word { get { return aWord; } set { aWord =
value; } }
}

public class B : A
{
[System.Xml.Serialization.XmlElementAttribute("b-word")]
public override string Word { get { return base.Word; } set {
base.Word = value; } }
... Other stuff...
}

The idea here is that when I serialize an instance of A, I want the
element corresponding to the Word property to show up as "<a-word>"
but when I serialize an instance of B, I want to have that property
serialized as "<b-word>". When the XmlSerializer goes to serialize
it, I get the following:

System.InvalidOperationException: Member 'B.Word' hides inherited
member 'A.Word', but has different custom attributes.

I've also tried making a new property that access the old one and
telling it to ignore the inherited one, but it still serializes the
inherited one anyway, which I dont want. for example:

public class B : A
{
[System.Xml.Serialization.XmlElementAttribute("b-word")]
public virtual string NewWord { get { return base.Word; } set {
base.Word = value; } }

[System.Xml.Serialization.XmlIgnoreAttribute()]
public override string Word { get { return base.Word; } set {
base.Word = value; } }

}

This yields two elements (one a-word and one b-word.) I want to hide
the a-word all together.

Now I've done a bunch of work with the design time components where
overriding declarative attributes on inheritance is common practice.
And furthermore I dont think this is a language problem. Is this just
not possible? Obviously I can reflect on it just prior to
serialization and remove the attributes at runtime, but the nicest
part of the XmlSerialization is that you can do all this stuff
declaratively, and not have to do a ton of footwork. Am I expecting
too much?

Ian
Nov 11 '05 #1
0 5133

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tony Johansson | last post: by
reply views Thread by Craig Edmunds | last post: by
reply views Thread by A programmer desperatly needing help! | last post: by
5 posts views Thread by Chris Szabo | last post: by
6 posts views Thread by tshad | last post: by
4 posts views Thread by vivekian | last post: by
3 posts views Thread by LW | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.