2

Here is a piece of sample code to explain my question:

public class TheBaseClass 
{
   public list<int> BaseClassList {get; set;}
}

public class TheDerivedClass : TheBaseClass
{
   //here I want to indicate the XmlSerializer to serialize the 'BaseClassList' with a different name 'DerivedClassList'
}

I know how to do this when the variable is in the same class by using [XmlElement( ElementName = "DesiredVarName")] but want to know if it is possible to do this in a derived class at all? If yes, how?

Umar T.
  • 411
  • 6
  • 11
  • Why do you want this? may be there is some other better way of achieving what you want to achieve – Nitin Apr 18 '16 at 08:25
  • Since the 'TheDerivedClass' is an old class already used in production environment, so there are many dependencies on it i.e. other clients are using it. But now with some additional requirements the implementation model has to evolve and some functionality + fields need to move to the base class, but with some new names that suit better to the overall model and to the other derived classes. So the serializeable fields of the 'TheDerivedClass' must retain their old names when serialized. – Umar T. Apr 18 '16 at 08:33

2 Answers2

1

From your comment, it appears you are able to make changes to TheBaseClass. Thus you can add a virtual bool ShouldSerialize{PropertyName}() method for the BaseClassList property in the base class and return true. Then override it in the derived class and return false, and introduce a proxy property with the desired name:

public class TheBaseClass
{
    public List<int> BaseClassList { get; set; }

    public virtual bool ShouldSerializeBaseClassList() { return true; }
}

public class TheDerivedClass : TheBaseClass
{
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public List<int> DerivedClassList { get { return BaseClassList; } set { BaseClassList = value; } }

    public override bool ShouldSerializeBaseClassList() { return false; }
}

For an explanation of why this works see Defining Default Values with the ShouldSerialize and Reset Methods.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Brilliant! That was what I was looking for. I'm aware of the usage of ShouldSerialze{PropertyName} pattern but somehow couldn't figure out the approach you've built using it. Thanks for the help! – Umar T. Apr 28 '16 at 16:32
0

One thing that comes to mind is to use XmlAttributeOverrides:

var attributes = new XmlAttributes();
attributes.XmlElements.Add(new XmlElementAttribute("DerivedClassList"));
var overrides = new XmlAttributeOverrides();
overrides.Add(typeof(TheBaseClass), "BaseClassList", attributes);

var serializer = new XmlSerializer(typeof(TheDerivedClass), overrides);

In this example we are programatically passing to the XmlSerializer a list of custom serialization attributes that will be applied.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for you suggestion, that basically does seem to rename it in the derived class, but unfortunately in my particular case i can't modify the serializer as its written in a generic way (also in another class) to support all other types of serialization needs of the model. So I can't customize it for a single case or at least I don't know how to use your approach without having to change anything in the general serializer. – Umar T. Apr 18 '16 at 10:00
  • Then I am afraid that there's not much that can be done. – Darin Dimitrov Apr 18 '16 at 10:28