'How to detect and remove duplicates from List in XmlArray (when reading in)

This is one attribute of a larger XML object (expressed in C#):

[XmlArray]
public List<CustomAssignment> CustomAssignments
{
    get => _CustomAssignments; set { _CustomAssignments = value; }
}
private List<CustomAssignment> _CustomAssignments = new List<CustomAssignment>();

The CustomAssignment object is:

public class CustomAssignment
{
    [XmlAttribute]
    public int Index
    {
        get => _Index; set => _Index = value;
    }
    private int _Index;
}

I have noticed that sometimes I have duplicates in the XML data. For example:

<CustomAssignments>
  <CustomAssignment Index="0" />
  <CustomAssignment Index="0" />
</CustomAssignments>

I now know why it has been happening, so I can prevent it in the future. But is there an easy way to delete this duplicates from my List?

Can this be acheived when the XML file is being serialized in?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source