1

请考虑以下xml。我想反序列化。但是,它不会反序列化规则元素。一些快速提示表示赞赏。

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

}
4

1 回答 1

0

在我的项目中,我有类似的情况,与您的代码的唯一区别是我设置了 XmlArrayItem 属性:

    [XmlArrayItem("Rules")]
    public List<Rule> Rules
    {
        get { return (_rules); }
        set { _rules = value; }
    }
于 2013-02-13T16:08:04.403 回答