XmlSerializer fails when trying to serialize hidden property with XmlIgnore

  • Thread starter Thread starter Gnaget
  • Start date Start date
G

Gnaget

Guest
I recently received a requirement to take a class which has been binary serialized, and in one case serialize it as XML, while retaining the current binary serialization. The current object structure is as follows (I cannot change these classes)

[Serializable]
public class BaseClass
{
public IDictionary<string, SomeType> BadProperty { get; set; }
}

[Serializable]
public class ImplementationClass : BaseClass
{
}




As you can see, the base class contains a property that cannot be XML Serialized. My solution is to create a new class which inherits from the ImplementationClass, and is more XML friendly. In this class I can hide properties I do not need, and wrap properties I do need with types that cannot be XML serialized


[Serializable]
public class SerializedClass : ImplementationClass
{
[XmlIgnore]
public new IDictionary<string, SomeType> BadProperty {get; set;}
}


As you can see with this example, I am hiding the property with the new keyword, and using the XmlIgnore attribute to prevent it from being serialized.

However, when I instantiate the XmlSerializer with


var s = new XmlSerializer(typeof(SerializedClass));

An exception is thrown:

Cannot serialize member 'BaseClass.BadProperty' of type...

It shouldn't even be trying to serialize BadProperty.




Continue reading...
 
Back
Top