C# – Serialization of class derived from List<> using DataContract

cdatacontractserialization

I'm trying to serialize a class derived from List<> using DataContract. The problem is that properties of my class won't get serialized.

My classes:

[CollectionDataContract] /*[Serializable]*/ /*[DataContract]*/
public class DownloadRuleCollection : List<DownloadRule> {

    [DataMember(EmitDefaultValue = false)]
    public string SomeProperty { get; set; }
    //this is, in fact, more complex, but this is enough for the example
}

[DataContract]
public class DownloadRule {
    [DataMember(EmitDefaultValue = false)]
    public string Name { get; set; }

    /*
     more properties
     ...
     */
}

Test:

static void Main(string[] args) {

    //fill test collection with some data...
    var col = new DownloadRuleCollection { SomeProperty = "someText" };

    var rule = new DownloadRule { Name = "test01" };
    col.Add(rule);

    rule = new DownloadRule { Name = "test02" };
    col.Add(rule);

    rule = new DownloadRule { Name = "test03" };
    col.Add(rule);

    //serialize
    Console.WriteLine("serializing");

    Serialize(col, "serializationTest.xml");

    Console.WriteLine("serialized");
    Console.ReadLine();
}

result:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDownloadRule xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1">
  <DownloadRule>
    <Name>test01</Name>
  </DownloadRule>
  <DownloadRule>
    <Name>test02</Name>
  </DownloadRule>
  <DownloadRule>
    <Name>test03</Name>
  </DownloadRule>
</ArrayOfDownloadRule>

As you can see, the List's items are serialized (and deserialized) properly, but the List itself does not get serialized. I have tried to use different attributes:
[Serializable], no change;
[DataContract], throws exception during serialization (collections cant use this attribute)

btw I am serializing also private fields, so I cannot use XmlSerializer (or other classes that can't serialize private fields).

Best Answer

Use an IList instead. That should serialize properly.

    [CollectionDataContract] /*[Serializable]*/ /*[DataContract]*/
    public class DownloadRuleCollection : IList<DownloadRule> {

Here is an example i use and works perfectly:

    [DataContract(Namespace="http://schemas.datacontract.org/2004/07/InboundIntegration.HL7Messaging")]
    public class Message {

        public Message() {
            InsuranceList = new List<Insurance>();
            MessageId = GuidComb.NewGuid();
        }

        [IgnoreDataMember]
        public Guid MessageId { get; private set; }

        #region "Data"
        [DataMember]
        public string MessageTypeIndicator { get; set; }
        [DataMember]
        public MessageConfiguration MessageConfiguration { get; set; }
        [DataMember]
        public Patient Patient { get; set; }
        [DataMember]
        public Encounter Encounter { get; set; }
        [DataMember]
        public IList<Insurance> InsuranceList { get; set; }
        #endregion

Then insurance class looks like this:

 [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/InboundIntegration.HL7Messaging")]
    public class Insurance {
        [DataMember]
        public string ExternalPayerId { get; set; } 
        [DataMember]
        public string PayerName { get; set; }
        [DataMember]
        public string GroupNumber { get; set; }
        [DataMember]
        public string MemberIdOfPatient { get; set; }
        [DataMember]
        public string PatientRelationshipToInsuredIndicator { get; set; }
        [DataMember]
        public string CoordinationOfBenefitsPrecedenceIndicator { get; set; }
Related Topic