2013-02-13 224 views
1

請考慮以下xml。我想反序列化。但是,它並沒有對規則元素進行反序列化。一些快速提示表示讚賞。XML序列化和反序列化嵌套元素c#

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

} 

回答

0

在我的項目,我有一個類似的條件,從你的代碼的唯一區別是,我有XmlArrayItem屬性設置:

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