I have XML in the following format:
<LocationHierarchy>
<Location name="Argentina" id="3">
<Location name="Buenos Aires" id="4"/>
<Location name="Cordoba" id="5"/>
<Location name="Mendoza" id="6"/>
</Location>
...
</LocationHierachy>
I have C# code that is deserializing this XML into the following classes:
[XmlRoot("LocationHierarchy")]
[Serializable]
public class LocationHierachy
{
[XmlElement("Location", typeof(Country))]
public List<Country> CountryList { get; set; }
}
public class Country
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("id")]
public int Id { get; set; }
[XmlElement("Location", typeof(City))]
public List<City> CityList { get; set; }
}
public class City
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("id")]
public int Id { get; set; }
public int CountryId { get; set; }
}
I have this working fine. However, I would like to automatically set the CountryId of each City object to the Id of the Country object that contains its collection. Any thoughts as to how this could be achieved?