将Xml反序列化为对象时出错 - 相同的实体名称

时间:2012-04-03 07:43:35

标签: c# xml serialization

XML我试图反序列化

<x>
    <result result="1" />
    <result redirect="http://blahblahblah.com/?data=a" />
    <result memo1="foo" />
    <result memo2="bar" />
             .
             .
             .
</x>

我正在尝试反序列化为:

[XmlRoot("x"), XmlType("x")]
public class EmptyModel
{
    [XmlArray("x")]
    [XmlArrayItem("result")]
    public List<ResultModel> result { get; set; }
}

[XmlType("result")]
public class ResultModel
{
    [XmlAttribute]
    public string redirect { get; set; }
    [XmlAttribute]
    public string err_code { get; set; }
    [XmlAttribute]
    public string err_detail { get; set; }
    [XmlAttribute]
    public string memo1 { get; set; }
    [XmlAttribute]
    public string memo2 { get; set; }
    [XmlAttribute]
    public string result { get; set; }
}

但我无法纠正使模型绑定。

var a = xml.ToXmlDeserialize<EmptyModel>();

如何制作合适的模型?

1 个答案:

答案 0 :(得分:1)

试试这个

[XmlRoot("x")]
public class EmptyModel
{
    [XmlElement()]
    public List<ResultModel> result { get; set; }
}