反序列化带有属性的XML数组



我必须从工具中消费xml。在xml中是这一部分:

<profile>
<details environment="sd98qlx" severity="critical">
<detail unit="sec" value="12"/>
<detail unit="sec" value="25"/>
<detail unit="msec" value="950"/>
</details>
</profile>

对于在c#中使用XMLSerializer反序列化上述内容,我应该如何在模型类上使用此属性?问题是数组和属性的组合。

使用示例模型类作为方向

[XmlRoot(ElementName = "detail")]
public class Detail
{
[XmlAttribute(AttributeName = "unit")]
public string Unit { get; set; }
[XmlAttribute(AttributeName = "value")]
public int Value { get; set; }
}
[XmlRoot(ElementName = "details")]
public class Details
{
[XmlElement(ElementName = "detail")]
public List<Detail> Detail { get; set; }
[XmlAttribute(AttributeName = "environment")]
public string Environment { get; set; }
[XmlAttribute(AttributeName = "severity")]
public string Severity { get; set; }
}
[XmlRoot(ElementName = "profile")]
public class Profile
{
[XmlElement(ElementName = "details")]
public Details Details { get; set; }
}

最新更新