I am having the following xml file :
<ItemList>
<Item id="item-aa-00" status="TYPE1">
<Elements>
<element>element121</element>
</Elements>
</Item>
<Item id="item-aa-01" status="TYPE2">
<Elements>
<element>element122</element>
<element>element123</element>
<element>element124</element>
</Elements>
</Item>
</ItemList>
I am trying to deserialize it using the following model :
[DataContract]
public enum ItemStatusEnum
{
[EnumMember(Value = "TYPE1")]
TYPE1 = 10,
[EnumMember(Value = "TYPE2")]
TYPE2 = 20
}
[XmlRoot("Item")]
[DataContract(Name = "Item")]
public class ItemDto
{
[DataMember(Name = "id")]
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[DataMember(Name = "candidateElements")]
[XmlIgnore]
public IEnumerable<string> Elements { get; set; }
[XmlArray("Elements")]
[XmlArrayItem("element"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public List<string> CandidateElementsSurrogate { get { return Elements .ToList(); } set { Elements = value; } }
[DataMember(Name = "status")]
[XmlAttribute(AttributeName = "status")]
public ItemStatusEnum Status { get; set; }
public ItemDto()
{
this.Elements = new List<string>();
}
}
[XmlRoot("ItemList")]
[DataContract(Name = "ItemListDto", Namespace = "")]
public class ItemListDto
{
[XmlElement("Item")]
[DataMember(Name = "myElements")]
public List<ItemDto> myElements { get; set; }
}
This model manages to deserialize my xml file, but the Elements collection remains empty. What i am missing ?
I have changed this two properties and could deserialize your xml:
[XmlIgnore]
public List<string> Elements { get; set; }
[XmlArray("Elements")]
[XmlArrayItem("element")]
public List<string> CandidateElementsSurrogate { get { return Elements; } set { Elements = value; } }
i have removed DataMember
Attributes, they are not used by deserialization.