嵌套列表项反映类型错误

嵌套列表项反映类型错误

问题描述:

我有一个包含变量的模型和其他类实例的列表.. 这里型号:嵌套列表项反映类型错误

public class patient 
    { 
     [XmlElement("firstname")]  
     public string name { get; set; } 
     [XmlElement("lastname")] 
     public string surname { get; set; } 
     [XmlElement("age")] 
     public int age { get; set; } 
     [XmlElement("gender")] 
     public string gender { get; set; } 
     [XmlArray("exams"), XmlArrayItem(typeof(exam), ElementName = "exam")] 
     public List<exam> exam { get; set; } 

    } 
    public class exam 
    { 
     [XmlElement("id")] 
     public string id { get; set; } 
     [XmlElement(ElementName = "date", DataType = "DateTime")] 
     public DateTime date { get; set; } 
     [XmlElement("comment")] 
     public string comment { get; set; } 
    } 







List<exam> examsLocal = new List<exam>(){ 
       new exam{ id = "id of patient 1", date = DateTime.Now, comment = "coomment exam1" }, 
      }; 
     List<patient> overview = new List<patient>(); 
     try 
     { 
      var b = new List<patient>() 
      { 
       new patient{ name = "name of patient 1", surname = "surname of patient 1", gender = "Female", age = 31, exam=examsLocal }, 
      }; 
      var writer = new System.Xml.Serialization.XmlSerializer(typeof(List<patient>));//throw exception 

行抛出异常工作正常,如果我删除“考试= examsLocal从列表'varible ..

什么是序列化嵌套列表项的正确方法

更新的exam类定义是看起来像: 删除日期属性的数据类型,因为System.DateTime的不是XML序列化允许:

public class exam 
{ 
    [XmlElement("id")] 
    public string id { get; set; } 
    [XmlElement(ElementName = "date")] 
    public DateTime date { get; set; } 
    [XmlElement("comment")] 
    public string comment { get; set; } 
} 

而且更新财产申报中patiant类:

[XmlArray("exams"), XmlArrayItem(typeof(exam), ElementName = "exams")] 
    public List<exam> exams { get; set; }