【发布时间】:2014-03-18 04:00:17
【问题描述】:
我有以下格式的 XML:
<LocationHierarchy>
<Location name="Argentina" id="3">
<Location name="Buenos Aires" id="4"/>
<Location name="Cordoba" id="5"/>
<Location name="Mendoza" id="6"/>
</Location>
...
</LocationHierachy>
我有将这个 XML 反序列化为以下类的 C# 代码:
[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; }
}
我有这个工作正常。但是,我想自动将每个 City 对象的 CountryId 设置为包含其集合的 Country 对象的 Id。关于如何实现这一点的任何想法?
【问题讨论】:
标签: c# .net xml xml-serialization deserialization