Ignoring derived class properties when using .NET XmlSerializer

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;}

   //and other properties 

   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();  
   }
}

//A cart that is populated by algo based on parameters supplied by user. I have no need to
//persist the actual items across sessions.
class DynamicCart: ShoppingCart
{
   [XmlIgnore]
   public override List<items>{get;set;}
   //and other properties 
}

class Shop
{
   ShoppingCart cart = new DynamicCart();
   PopulateCart(cart);
   cart.serializeToXML(<PATH TO FILE>);
}
+5
4

, .

public void SerializeToXML(string filePath, Type type)
{
    xmlSerializer = new XmlSerializer(type);
    textWriter = new System.IO.StreamWriter(filePath);
    xmlSerializer.Serialize(textWriter, this);
    textWriter.Flush();
    textWriter.Close();
}

class Shop
{
    ShoppingCart cart= new DynamicCart();
    PopulateCart(cart);
    cart.serializeToXML(<PATH TO FILE>, typeof(DynamicCart));
}
0

, ShouldSerialize*** . :

[XmlInclude(typeof(Sub))]
public class Base
{
    public virtual string Prop { get; set; }

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

public class Sub : Base
{
    public override string Prop { get; set; }

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

internal class Program
{
    private static void Main()
    {
        var o = new Sub { Prop = "Value" };

        var baseSer = new XmlSerializer(typeof (Base));
        var subSer = new XmlSerializer(typeof (Sub));

        Console.Out.WriteLine("BASE:");
        baseSer.Serialize(Console.Out, o);
        Console.Out.WriteLine();

        Console.Out.WriteLine("SUB:");
        subSer.Serialize(Console.Out, o);
        Console.Out.WriteLine();

        Console.ReadLine();
    }
}

( ):

BASE:
<Base xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Sub">
  <Prop>Value</Prop>
</Base>

SUB:
<Sub />

, ShouldInclude....

0

, XML. , .

. MSDN :

[System.Xml.Serialization.XmlInclude( typeof( Derived ) )]
public class Base
{
    // ...
}
-1

  XmlSerializer serializer = new XmlSerializer(typeof(DynamicCart), new Type[]{typeof(ShoppingCart)});

this will allow you to add as many types that you want the serializer to turn on.

-1
source

All Articles