简体   繁体   中英

Deserialize XML nested lists to nested ObservableCollection

I am building an object that contains an ObservableCollection within an ObservableCollection . Deserializing the object below works correctly to create the collection of CustomTab items but not Task items.

The code shown below produces zero Task items. 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).

How can I get TasksCollection populated with all the Task items under the respective Tasks root?

NOTE : I have simplified all of the Properties to just { get; set; } { get; set; } { get; set; } for readability. In code I have each one sending notification of any changes.

I am using the SimpleMVVM Framework which is where ModelBase<> comes from.

Here is my code:

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

<?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)

[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

    [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:

[XmlArray("Tasks")]
[XmlArrayItem("Task", typeof(UtilitiesTask))]
public ObservableCollection<UtilitiesTask> TaskCollection

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