将xml反序列化为类,遇到列表的问题
我有以下XML
我正在尝试编写可以将其反序列化的类,这就是我所拥有的:
[XmlRoot("map")] public class MyMap { [XmlAttribute("version")] public decimal Version { get; set; } [XmlElement("properties")] public List Properties { get; set; } } public class MyProperty { [XmlAttribute("name")] public string Name { get; set; } [XmlAttribute("value")] public string Value { get; set; } }
问题是我似乎无法读取属性列表,我只得到一个条目,它在Name和Value中都为null。
我需要设置一些神奇的属性来使其工作吗?
您应该如下更改MyMap。 XmlArray
和XmlArrayItem
是神奇的属性
[XmlRoot("map")] public class MyMap { [XmlAttribute("version")] public decimal Version { get; set; } [XmlArray("properties")] [XmlArrayItem("property")] public List Properties { get; set; } }
而不是XmlElement,尝试:
[XmlArray("Properties")]
…在List <>属性上。
找到解决方案的一种方法是在代码中填充对象,然后将其序列化为xml,并查看模式的外观。 您也可以使用xsd.exe自动生成类。