我有以下 xml 结构,我试图反序列化为 ac# 对象:
<leagueTable>
<competition>Calor League Division One Central</competition>
<description>League Table</description>
<team>
<position>1</position>
<name>Barton Rovers</name>
<played>13</played>
<won>8</won>
<drawn>3</drawn>
<lost>2</lost>
<for>25</for>
<against>16</against>
<goalDifference>+9</goalDifference>
<points>27</points>
</team>
<team>
......
</team>
<team>
.....
</team>
</leagueTable>
并为它们创建了两个要反序列化的类,如下所示
public class leagueTable
{
public string competition { get; set; }
public string description { get; set; }
[XmlElement("team")]
public List<team> TeamsList { get; set; }
}
和
[Serializable()]
public class team
{
public int position { get; set; }
public String name { get; set; }
public int played { get; set; }
public int won { get; set; }
public int drawn { get; set; }
public int lost { get; set; }
[XmlElement("for")]
public int goalsfor { get; set; }
[XmlElement("against")]
public int goalsagainst { get; set; }
[XmlElement("goalDifference")]
public int goaldifference { get; set; }
public int points { get; set; }
}
我似乎无法弄清楚如何将团队反序列化为列表对象。当反序列化发生时,会填充竞争和描述属性,但是团队列表列表对象为空。我尝试了多种不同的方法来创建属性,但每种方法要么失败,要么保持为空。我在这里和网络上关注了许多不同的帖子,但无论我尝试什么,它仍然无法填充。
为了完整起见,这是反序列化代码,经过编辑以显示 xml 设置
XmlSerializer serializer = new XmlSerializer(typeof(leagueTable));
leagueTable league = null;
Stream xmldoc = response.GetResponseStream();
XmlReaderSettings xmlsettings = new XmlReaderSettings();
xmlsettings.DtdProcessing = DtdProcessing.Parse;
using (XmlReader reader = XmlReader.Create(xmldoc, xmlsettings))
{
league = (leagueTable)serializer.Deserialize(reader);
}