[英]How to deserialize this XML (List inside Class)?
我有以下XML:
<CustomTabsConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CustomTab>
<Header>555</Header>
<TabIsVisible>true</TabIsVisible>
<Tasks>
<Task>
<TaskLabel>Task 23</TaskLabel>
<ButtonLabel />
<ButtonType />
<TaskParameters />
</Task>
<Task>
<TaskLabel>Task 22</TaskLabel>
<ButtonLabel />
<ButtonType>CrystalReports</ButtonType>
</Task>
<Task>
<TaskLabel>Task 21</TaskLabel>
<ButtonLabel />
<ButtonType />
<TaskParameters />
</Task>
</Tasks>
</CustomTab>
</CustomTabInfo>
我需要将其反序列化为以下对象(为清楚起见而简化):
// ####################################################
// CustomTab Model
// ####################################################
[XmlRoot("CustomTab")]
public class CustomTab
{
public CustomTab()
{
}
[XmlElement("Header")]
public String Header { get; set; }
[XmlElement("TabIsVisible")]
public Boolean TabIsVisible { get; set; }
[XmlIgnore]
public TaskCollection TaskCollection { get; set; }
}
// ####################################################
// TaskCollection Model
// ####################################################
public class TaskCollection
{
public TaskCollection()
{
TaskList = new List<UtilitiesTask>();
}
public List<UtilitiesTask> TaskList { get; set; }
}
// ####################################################
// UtilitiesTask Model
// ####################################################
public class UtilitiesTask
{
public UtilitiesTask()
{
}
[XmlElement("TaskLabel")]
public String TaskLabel { get; set; }
[XmlElement("ButtonLabel")]
public String ButtonLabel { get; set; }
[XmlElement("ButtonType")]
public TaskButtonTypeEnums? ButtonType { get; set; }
}
如何获取此XML以反序列化到该对象中? 我坚持的是如何声明TaskCollection
和TaskList
以便它们填充<Tasks>
和<Task>
对象。
由于对该项目有一些其他限制,我不能简单地使TaskCollection在CustomTab中成为List对象。
我知道如果TaskCollection是CustomTab下的List,以下方法将起作用:
[XmlArray("Tasks")]
[XmlArrayItem("Task", typeof(UtilitiesTask))]
public List<UtilitiesTask> TaskList { get; set; }
感谢Sinatr向我指出相关文章。 我通过更改以下项目解决了我的问题:
//[XmlIgnore] - removed this line and added the next line
[XmlElement("Tasks")]
public TaskCollection TaskCollection { get; set; }
[XmlElement("Task", typeof(UtilitiesTask))]
public List<UtilitiesTask> TaskList { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.