asp.netxmlentity-frameworkxml-serializationicollection

XMLserializer, Entity Framework : Cannot serialize member of type ICollection see inner exception for more details


I want to map XML elements into my database table (using Entity Framework):

var xmlSerializer = new XmlSerializer(typeof(Participant), new XmlRootAttribute("participant"));
var participant = (Participant)xmlSerializer.Deserialize(new StringReader(content));

I have Participant table which I can access by

[XmlRoot("participant", Namespace = "")]
    public partial class Participant
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",     "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Participant()
        {
            this.GroupParticipant = new HashSet<GroupParticipant>();
            this.ParticipantAddress = new HashSet<ParticipantAddress>();
            this.ParticipantPublisher = new HashSet<ParticipantPublisher>();
            this.ParticipantJob = new HashSet<ParticipantJob>();
            this.ParticipantProvider = new HashSet<ParticipantProvider>();
        }
        [XmlElement("firstName")]
        public string FirstName { get; set; }

        [XmlElement("lastName")]
        public string LastName { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        //[XmlElement("address")]
        //[XmlElement("address")]
        //[XmlArray("HashSet<ParticipantAddress>"), XmlElement("address")]
        //[XmlArrayItem("ICollection<ParticipantAddress>")]
        //[XmlAttribute(DataType = "ICollection<ParticipantAddress>",  AttributeName = "address")]

[XmlElement("address", typeof(List<ParticipantAddress>))]
        public virtual ICollection<ParticipantAddress> ParticipantAddress { get; set; }
}

ParticipantAddress is ICollection:

[Serializable]
    [XmlInclude(typeof(HashSet<ParticipantAddress>))]
    public partial class ParticipantAddress
    {
        public int ParticipantAddressId { get; set; }
        public int ParticipantId { get; set; }

        [XmlElement("city")]
        public string City { get; set; }

        [XmlElement("state")]
        public string State { get; set; }

        [XmlElement("zipCode")]
        public string ZipCode { get; set; }

        public virtual Participant Participant { get; set; }
    }

Exception says:

{"There was an error reflecting type 'x.Participant'."}

My inner Exception says:

{"Cannot serialize member 'xParticipant.ParticipantAddress' of type 'System.Collections.Generic.ICollection`1[[x.ParticipantAddress, APS.Data.BatchInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details."}

I am reading XML by streamReader.

I have tried

Is there any other way to overcome this problem or any examples related to my question or any changes I need to implement in my code?


Solution

  • ICollection is not serializable.
    - You can use DTO.
    - You can change the collection type (i.e. with List<>) and with XML serialization attributes avoid circular references and/or disable lazy load (i.e. use eagerly load using Include method) or the risk is that you serialize the whole database.