反序列化具有属性的 XML 数组
Deserializing an XML array with attributes
我必须从工具中消耗 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 反序列化上述内容,我应该如何在我的模型上为此使用属性 class?问题是数组与属性的组合。
方向使用示例模型class
[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; }
}
我必须从工具中消耗 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 反序列化上述内容,我应该如何在我的模型上为此使用属性 class?问题是数组与属性的组合。
方向使用示例模型class
[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; }
}