I have a base class with a virtual property and a derived type that overrides the virtual property. A type can be serialized in XML. What I'm trying to do is NOT save the List of items property when the object is of a derived type. To achieve this, a derived class decorates the overridden property with an attribute [XmlIgnore]. The virtual property in the base class does NOT apply the attribute XmlIgnore. For some reason, the list of elements gets serialized each, even if the object has a derived type ( DynamicCart).
When I apply an attribute XmlIgnoreto a virtual property in the base class, the list is not converted to a file.
public class ShoppingCart
{
public virtual List<items> Items{get; set;}
public void SerializeToXML (string filePath)
{
var xmlSerializer = new XmlSerializer(this.GetType());
textWriter = new System.IO.StreamWriter(filePath);
xmlSerializer.Serialize(textWriter, this);
textWriter.Flush();
textWriter.Close();
}
}
class DynamicCart: ShoppingCart
{
[XmlIgnore]
public override List<items>{get;set;}
}
class Shop
{
ShoppingCart cart = new DynamicCart();
PopulateCart(cart);
cart.serializeToXML(<PATH TO FILE>);
}