views:

1929

answers:

3

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?

+1  A: 

Depending on exactly what you are doing, there are a variety of options... Investigate the Xml Attributes in System.Xml.Serialization namespace... In particular, check out

[XmlArrayItem(ElementName = "")]

This goes on the property of a class that is typed as a collection of some kind (I think it needs to implement IList) and will be populated by the XmlDeserializer with Xml elements named "ElementName"...

There are a whole bunch of Xml Attributes inm this namespace that you can use to exactly control how serialization and deserialization takes place. You can create just about any class structure you want, by the appropriate decoration with the right Xml*Atttributes

Charles Bretana
I do have the XmlArrayItem tag - I am wondering if the "ArrayList" of byte arrays is the problem.
Stuart Helwig
Thanks for the effort there Charles. The problem was not with my serialization in the end, making this question "a bit" difficult to answer, it was with the property itself. Sorry and thanks again for your effort.
Stuart Helwig
A: 

Properties of serializable classes, that are to be serialized, must be Read/Write.

In my case above, the ArrayList property to read only, It was returning byte arrays based on a separate function where file names were added to it.

Once a setter was written for ArrayList property and the logic tweaked a bit throughout the Add method, the serializing works.

Lesson: for serializable classes that you need to reconstruct based on the serialised stream, all serialized properties need to be writable - kinda obvious when you say it lke that.

Stuart Helwig
Actually, this has been a long-standing gripe of mine... I'm not so sure it *is* obvious. It isn't really good practice to have a setter on your list - normally you expect people to use ".Items.Add" etc, but not ".Items =" (except for arrays, maybe).
Marc Gravell
I've been working with xml serialization in .NET for quite a long time, and this still whiffs a bit. BTW - you probably shouldn't be using ArrayList all that much... prefer List<T> in anything 2.0 or above.
Marc Gravell
Thanks for your input Marc.
Stuart Helwig
A: 

great question - and great resolution. thnx for posting it, guys!

Dan
Maybe a vote rather than an "answer"?....Cheers.
Stuart Helwig