0

With the following EF Core entity class, I am unable to populate Orders by utilizing XmlSerializer.Deserialize()

OrderIds does serialize correctly, but the setter never gets called. I'm doing this to avoid a circular reference in the XML.

[Serializable]
public class Customer
{
    public Customer()
    {
        this.Orders = new HashSet<Order>();
    }
        
    [XmlIgnore]
    public virtual ICollection<Order> Orders { get; }
    
    [NotMapped]
    [XmlArray("OrderIds")]
    [XmlArrayItem("OrderId")]
    public List<string> OrderIds
    {
        get => this.Orders
                .Select(o => o.OrderId).ToList();
        
        set // Never Called
        {
            this.Orders.Clear();
            foreach (var id in value)
            {
                var order = new Order()
                {
                    OrderId = id,
                };
                this.Orders.Add(order);
            }
        }
    }
}

Here's an XML Sample, generated with XmlSerializer.Serialize():

<Report>
    <Customer>
        <Name>John Smith</Name>
        <Age>33</Age>
        <OrderIds>
            <OrderId>A</OrderId>
            <OrderId>B</OrderId>
        </OrderIds>
    </Customer>
    <Customer>
        <Name>Jane Doe</Name>
        <Age>33</Age>
        <OrderIds>
            <OrderId>C</OrderId>
            <OrderId>D</OrderId>
        </OrderIds>
    </Customer>
</Report>
Jackson
  • 87
  • 12
  • Does xml file contain one or two tags (OrderIDs and OrderID)? If there is only one than replace XmlArray and XmlArryItem with XmlElement. – jdweng Aug 03 '22 at 23:12
  • Xml file contains multiple `OrderId` elements nested under a single `OrderIds` per `Customer` element. I can post a snippet of the schema and xml in the mornining (I'm in eastern time) – Jackson Aug 04 '22 at 01:14
  • Need to see sample of XML. – jdweng Aug 04 '22 at 01:42
  • @jdweng, added XML sample – Jackson Aug 04 '22 at 13:15
  • Found this today, which answers my question: https://stackoverflow.com/questions/10281882/setter-not-called-when-deserializing-collection – Jackson Aug 04 '22 at 14:22

0 Answers0