Tuesday, 24 February 2015

How to deserialize this XML (List inside Class)?



I have the following XML:



<CustomTabsConfig xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<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>


I need to deserialize this into the following object (simplified for clarity):



// ####################################################
// 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; }
}


How can I get this XML to deserialize into this object? What I am stuck on is how to declare TaskCollection and TaskList so they are populated with <Tasks> & <Task> objects.


I cannot simply make TaskCollection a List object in CustomTab due to some other restrictions on this project.


I know the following would work if TaskCollection were a List under CustomTab:



[XmlArray("Tasks")]
[XmlArrayItem("Task", typeof(UtilitiesTask))]
public List<UtilitiesTask> TaskList { get; set; }

No comments:

Post a Comment