[英]Deserialize XML nested lists to nested ObservableCollection
I am building an object that contains an ObservableCollection
within an ObservableCollection
. 我建立一个包含对象ObservableCollection
的范围内ObservableCollection
。 Deserializing the object below works correctly to create the collection of CustomTab
items but not Task
items. 反序列化下面的对象可以正常创建CustomTab
项的集合,但不能创建Task
项。
The code shown below produces zero Task
items. 下面显示的代码产生零个Task
项。 If I change the code to: 如果我将代码更改为:
[XmlElement("Tasks")] // was "Task" in original code
public ObservableCollectionExtended<UtilitiesTask> TasksCollection
I get one item in TasksCollection (should be 2 or 3 elements depending on which CustomTab I am reading). 我在TasksCollection中得到一项(应为2或3个元素,具体取决于我正在阅读哪个CustomTab)。
How can I get TasksCollection populated with all the Task
items under the respective Tasks
root? 如何获得在各个Tasks
根目录下填充有所有Task
项目的TasksCollection?
NOTE : I have simplified all of the Properties to just { get; set; }
注意 :我已经将所有属性简化为{ get; set; }
{ get; set; }
{ get; set; }
for readability. { get; set; }
以提高可读性。 In code I have each one sending notification of any changes. 在代码中,我每个人都会发送任何更改的通知。
I am using the SimpleMVVM Framework which is where ModelBase<>
comes from. 我正在使用SimpleMVVM框架,它是ModelBase ModelBase<>
来源。
Here is my code: 这是我的代码:
CustomTabs.xml CustomTabs.xml
<?xml version="1.0" encoding="utf-8"?>
<CustomTabsConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CustomTab>
<Header>88888</Header>
<TabVisibility>Visible</TabVisibility>
<Tasks>
<Task>
<TaskName>Task 11</TaskName>
</Task>
<Task>
<TaskName>Task 12</TaskName>
</Task>
</Tasks>
</CustomTab>
<CustomTab>
<Header>555</Header>
<TabVisibility>Visible</TabVisibility>
<Tasks>
<Task>
<TaskName>Task 21</TaskName>
</Task>
<Task>
<TaskName>Task 22</TaskName>
</Task>
<Task>
<TaskName>Task 23</TaskName>
</Task>
</Tasks>
</CustomTab>
</CustomTabsConfig>
CustomTabs.xsd CustomTabs.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="CustomTabsConfig" type="CustomTabsConfigType"/>
<xs:complexType name="CustomTabsConfigType">
<xs:sequence>
<xs:element type="CustomTabType" name="CustomTab" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="CustomTabType">
<xs:sequence>
<xs:element type="xs:int" name="Header"/>
<xs:element type="xs:string" name="TabVisibility"/>
<xs:element type="TasksType" name="Tasks"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TasksType">
<xs:sequence>
<xs:element type="TaskType" name="Task" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TaskType">
<xs:sequence>
<xs:element type="xs:string" name="TaskName"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
CustomTabsConfigModel.cs (edited for brevity) CustomTabsConfigModel.cs (为简便起见进行编辑)
[XmlRoot("CustomTabsConfig")]
public class CustomTabsConfigModel : ModelBase<CustomTabsConfigModel>
{
private ConfigReader<CustomTabsConfigModel> configReader;
/// <summary>
/// Create a configuration object bound to "CustomTabs.xml"
/// contained in [CommonApplicationData]\[Manufacturer]\[ProductName]
/// </summary>
public CustomTabsConfigModel()
{
}
/// <summary>
/// Create a configuration object bound to the specified file name
/// contained in [CommonApplicationData]\[Manufacturer]\[ProductName]
/// </summary>
/// <param name="fileName"></param>
public CustomTabsConfigModel(String fileName)
{
this.TabsFileName = fileName;
}
/// <summary>
/// Collection of custom tabs
/// </summary>
[XmlElement("CustomTab")]
public ObservableCollection<CustomTab> CustomTabsCollection { get; set; }
/// <summary>
/// Loads Tab info from customtabs.xml file
/// </summary>
public Boolean LoadConfig(String customTabsFileName = "CustomTabs.xml")
{
// Load tabs file
configReader = new ConfigReader<CustomTabsConfigModel>(customTabsFileName);
// if we cannot load the config file then don't do anything else
if (configReader.Load())
{
// update all items from CustomTabs.xml
CustomTabsCollection = configReader.Data.CustomTabsCollection;
}
else
{
Log.Write(LogLevel.Fatal, "Configuration file failed to load.");
return false;
}
return true;
}
}
CustomTab.cs CustomTab.cs
[XmlRoot("CustomTab")]
public class CustomTab : ModelBase<CustomTab>
{
public CustomTab()
{
this.Header = "";
this.TabVisibility = Visibility.Visible;
}
[XmlElement("Header")]
public String Header { get; set; }
/// <summary>
/// Sets the Visibility of the tab
/// </summary>
[XmlElement("TabVisibility")]
public Visibility TabVisibility { get; set; }
/// <summary>
/// Collection of Tasks
/// </summary>
[XmlElement("Task")]
public ObservableCollection<UtilitiesTask> TasksCollection { get; set; }
}
[XmlRoot("Task")]
public class UtilitiesTask : ModelBase
{
public UtilitiesTask()
{
this.TaskName = "";
}
[XmlElement("TaskName")]
public String TaskName { get; set; }
}
}
I resolved this by updating the TasksCollection
like so: 我通过更新TasksCollection
解决此问题, TasksCollection
所示:
[XmlArray("Tasks")]
[XmlArrayItem("Task", typeof(UtilitiesTask))]
public ObservableCollection<UtilitiesTask> TaskCollection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.