Setter not called when deserializing collection

Answer for clarity:

Have done some debugging and found that XmlSerializer does not call the setter for a collection.

Instead is calls the getter, and then adds items to the collection returned. Thus a solution such as Felipe's is necessary.


Have you tried using the XmlArray attribute?

With your example it would be something like this:

[XmlArray]
[XmlArrayItem(ElementName="XmlPerson")]
public List<XmlPerson> XmlPeople

EDIT:

Here, try the following structure:

public struct XmlPerson
{
    [XmlAttribute] public string Id   { get; set; }
    [XmlAttribute] public string Name { get; set; }
}


public class GroupOfPeople
{
    [XmlArray]
    [XmlArrayItem(ElementName="XmlPerson")]
    public List<XmlPerson> XmlPeople { get; set; }
}

I don't think it will be easy to add code to the Setter of the list, so what about getting that Dictionary when you actually need it?

Like this:

private Dictionary<string, string> _namesById;

public Dictionary<string, string> NamesById
{
    set { _namesById = value; }
    get
    {
        if (_namesById == null)
        {
            _namesById = new Dictionary<string, string>();

            foreach (var person in XmlPeople)
            {
                 _namesById.Add(person.Id, person.Name);
            }
        }

        return _namesById;
    }
}

This way you'll get the items from the XML and will also mantain that Dictionary of yours.