C# - XML serialization of derived classes

后端 未结 1 1540
广开言路
广开言路 2020-12-15 12:00

I\'m trying to serialize a List of multiple elements (Suppliers, Customers, Products, etc), all deriving from the same class (MasterElement)

public class XML         


        
相关标签:
1条回答
  • 2020-12-15 12:27

    You can use XmlArrayItem to get around this:

    public class XMLFile
    {
        [XmlArray("MasterFiles")]
        [XmlArrayItem("Supplier", typeof(Supplier))]
        [XmlArrayItem("Customer", typeof(Customer))]
        public List<MasterElement> MasterFiles;
    }
    

    From the linked MSDN:

    The XmlArrayItemAttribute supports polymorphism--in other words, it allows the XmlSerializer to add derived objects to an array.

    0 讨论(0)
提交回复
热议问题