简体   繁体   中英

XML Serialization and Deserialization nested elements c#

Please consider following xml. I want to deserialize. But, it does not deserializes the Rules elements. Some quick hints are appreciated.

<ImportDefinition>
    <Import>
    <Customer>
        <Name>Customer1</Name>
        <Path>\\192.168.1.1\40014\</Path>
        <Rules>
           <Rule Type="OrgNr-VATNr">INDEX_6|BDPOS_7</Rule>  
           <Rule Type="DueDate">INDEX_11|BDPOS_7|25</Rule>
        </Rules>
    </Customer>
  </Import>
</ImportDefinition>

    [XmlRoot("ImportDefinition")]
    public class ImportDefinition
    {
        public ImportDefinition() { }

        [XmlArray("Import"), XmlArrayItem("Customer", typeof(Customer))]
        public List<Customer> Customers
        {
            get { return (_customers); }
            set { _customers = value; }
        }
    }


[XmlType(TypeName = "Customer"), Serializable]
    public class Customer
    {
        public Customer() { }

        [XmlElement("Name")]
        public string Name { get; set; }

        [XmlElement("Path")]
        public string Path { get; set; }

        private List<Rule> _rules = new List<Rule>();
        [XmlArray("Rules")]
        public List<Rule> Rules
        {
            get { return (_rules); }
            set { _rules = value; }
   }
}

[XmlType(TypeName = "Rule"), Serializable]
public class Rule
{
    public Rule() { }

    [XmlAttribute("Type")]
    public string Type { get; set; }

    [XmlElement("Rule")]
    public string Rule { get; set; }

}

In my project i have a similar condition and the only difference from your code is that i have the XmlArrayItem attribute set:

    [XmlArrayItem("Rules")]
    public List<Rule> Rules
    {
        get { return (_rules); }
        set { _rules = value; }
    }

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