我想生成以下xml输出
<account>
<id>123456</id>
<roles>
<role master-name="I80">
<id>88</id>
<unit>Implementer</unit>
</role>
<role master-name="S20">
<id>21</id>
<unit>Support</unit>
</role>
</roles>
</account>
所以我创建了以下类
[XmlRoot("account")]
public class Account
{
[XmlElement("id")]
public int ID { get; set; }
[XmlElement("roles")]
public RolesList Roles { get; set; }
}
public class RolesList
{
[XmlElement("role")]
public List<Role> roles { get; set; }
}
public class Role
{
[XmlAttribute("master-name")]
public string MasterName { get; set; }
[XmlElement("id")]
public int ID { get; set; }
[XmlElement("unit")]
public string Unit { get; set; }
}
我的问题是,有没有办法更好地代表这一点?我想避免创建RolesList类。
答案 0 :(得分:2)
您应该在Account
:
[XmlArray("roles")]
[XmlArrayItem("role")]
public List<Role> Roles { get; set; }