I have given XML that I can\'t change and I need to deserialize it to a custom class:
Adressess class should be declared this way:
[XmlRoot("Adressess")]
public class Adressess
{
[XmlElement(ElementName = "MainAddress")]
public MainAddress Main { get; set; }
[XmlElement(ElementName = "AdditionalAddress")]
public List<AdditionalAddress> AdditionalAddresses { get; set; }
}
[XmlRoot("MainAddress")]
public class MainAddress
{
public string Country { get; set; }
public string City { get; set; }
}
[XmlRoot("AdditionalAddress")]
public class AdditionalAddress
{
public string Country { get; set; }
public string City { get; set; }
}
Also if the root object type has a namespace, you must use the same namespace for the nested types.
Example:
[System.Xml.Serialization.XmlRootAttribute(Namespace = "some namespace")]
class Person {...}
[System.Xml.Serialization.XmlRootAttribute(Namespace = "some namespace")]
class MainAddress{...}