简体   繁体   中英

How to deserialize this XML (List inside Class)?

I have the following 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>

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

Thanks to Sinatr for pointing me to the related post. I resolved my issue by changing the following items:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM