I have the following sample XML:
<PossibleAddresses>
<Address tempid="12345">1 The Street England</Address>
<Address tempid="6789">2 The Street England</Address>
<Address tempid="4321">3 The Street England</Address>
<Address tempid="1111">4 The Street England</Address>
</PossibleAddresses>
I am trying to deserialize this XML so that I essentially just get back a list of 'Address' objects that contains two properties. One being the 'tempid' the other being the actual address string itself.
My class looked something like this:
[XmlRoot("PossibleAddresses")]
public class Addresses
{
[XmlArrayItem("Address", Type = typeof(Address))]
public List<Address> PossibleAddresses { get; set; }
}
public class Address
{
[XmlAttribute(AttributeName = "tempid")]
public string TempId { get; set; }
[XmlElement(ElementName = "Address")]
public string FullAddress { get; set; }
}
I am then using the XmlSerializer to deserialize the XML. With T being the class 'Addresses':
public static T DeserializeObject<T>(string data)
{
var ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(new StringReader(data));
}
Currently this will deserialize successfully. I just end up with an empty list of addresses.
PossibleAddressesto[XmlElement("Address", Type = typeof(Address))]public List<Address> PossibleAddresses { get; set; }