How do you deserialize a collection with child collections?

Posted by Stuart Helwig on Stack Overflow See other posts from Stack Overflow or by Stuart Helwig
Published on 2008-11-19T01:33:25Z Indexed on 2010/04/22 1:13 UTC
Read the original article Hit count: 247

I have a collection of custom entity objects one property of which is an ArrayList of byte arrays.

The custom entity is serializable and the collection property is marked with the following attributes: [XmlArray("Images"), XmlArrayItem("Image",typeof(byte[]))]

So I serialize a collection of these custom entities and pass them to a web service, as a string.

The web service receives the string and byte array in tact,

The following code then attempts to deserialize the collection - back into custom entities for processing...

XmlSerializer ser = new XmlSerializer(typeof(List<myCustomEntity>));
StringReader reader = new StringReader(xmlStringPassedToWS);
List<myCustomEntity> entities = (List<myCustomEntity>)ser.Deserialize(reader);

foreach (myCustomEntity e in entities)
{
    // ...do some stuff...

    foreach (myChildCollection c in entities.ChildCollection
    {
        // .. do some more stuff....
    }
}

I've checked the XML resulting from the initial serialization and it does contain byte array - the child collection, as does the StringReader built above.

After the deserialization process, the resulting collection of custom entites is fine, except that each object in the collection does not contain any items in its child collection. (i.e. it doesn't get to "...do some more stuff..." above.

Can someone please explain what I am doing wrong? Is it possible to serialize ArrayLists within a generic collection of custom entities?

© Stack Overflow or respective owner

Related posts about c#

Related posts about serialization