如何反序列化此 XML(List inside Class)?
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 下的列表,则以下内容会起作用:
[XmlArray("Tasks")]
[XmlArrayItem("Task", typeof(UtilitiesTask))]
public List<UtilitiesTask> TaskList { get; set; }
感谢 Sinatr 为我指点相关 post。我通过更改以下项目解决了我的问题:
//[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; }
我有以下 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 下的列表,则以下内容会起作用:
[XmlArray("Tasks")]
[XmlArrayItem("Task", typeof(UtilitiesTask))]
public List<UtilitiesTask> TaskList { get; set; }
感谢 Sinatr 为我指点相关 post。我通过更改以下项目解决了我的问题:
//[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; }