Serializing Collections.
When designing or using strongly-typed collection classes that are to be serialized over the wire with webservices and the xml serialization process, you should keep in mind that:
- Based on a couple of rules, your class will be serialized as an array of the type your collection is containing. This means that if you add additional properties to your collection, they will NOT be included. For example:
Here, CustomerAccounts will be serialized but it will be serialized as an array of Account objects. Name, Address, et al will not go across the wire. The obvious solution is to do do something like this:
Most people find the latter implementation more intuitive anyway. So few come across this issue but it's something to keep in mind. Also the following blurb was taken from MSDN
XmlSerializer can process classes that implement IEnumerable or ICollection differently if they meet certain requirements. A class that implements IEnumerable must implement a public Add method that takes a single parameter. The Add method's parameter must be consistent (polymorphic) with the type returned from the IEnumerator.Current property returned from the GetEnumerator method. A class that implements ICollection in addition to IEnumerable (such as CollectionBase) must have a public Item indexed property (an indexer in C#) that takes an integer, and it must have a public Count property of type integer. The parameter passed to the Add method must be the same type as that returned from the Item property, or one of that type's bases. For classes implementing ICollection, values to be serialized will be retrieved from the indexed Item property rather than by calling GetEnumerator. Also note that public fields and properties will not be serialized, with the exception of public fields that return another collection class (one that implements ICollection). For an example, see Examples of XML Serialization.