【发布时间】:2016-06-13 03:28:39
【问题描述】:
这是我用来保存序列化数据的模型。当我从 xml 运行主要属性(姓名、姓氏..)设置得很好但嵌套对象(考试)时,属性(id、日期、评论)为空在里面
代码中的什么解决了这个问题?
namespace WpfApplication1
{
[Serializable, XmlRoot("patients")]
public class patients
{
[XmlElement("patient")]
public List<patient> patients_list { get; set; }
}
public class patient
{
[XmlElement("firstname")]
public string name { get; set; }
[XmlElement("lastname")]
public string surname { get; set; }
[XmlElement("age")]
public int age { get; set; }
public string gender { get; set; }
[XmlElement("exams")]
public List<exam> exam { get; set; }
}
[XmlRoot("exams")]
public class exam
{
[XmlElement("id")]
public int id { get; set; }
public DateTime date { get; set; }
[XmlElement("comment")]
public string comment { get; set; }
}
}
以及我进行序列化的主要代码:
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(patients));
System.IO.StreamReader file = new System.IO.StreamReader("data.xml");
var asd = (patients)reader.Deserialize(file);
和xml文件:
<patients>
<patient>
<firstname>Patience_name_1</firstname>
<lastname>Patience_surname_1</lastname>
<age>20</age>
<gender>Male</gender>
<exams>
<exam>
<id>1</id>
<date>2/29/2016 12:18:44</date>
<comment value="patiente">Exam completed for patience1</comment>
</exam>
</exams>
</patient>
<patient>
<firstname>Patience_name_2</firstname>
<lastname>Patience_surname_2</lastname>
<age>22</age>
<gender>Male</gender>
<exams>
<exam>
<id>2</id>
<date>2/29/2016 12:18:44</date>
<comment value= "sdsad">Exam completed fro patience 2</comment>
</exam>
</exams>
</patient>
<patient>
<firstname>Patience_name_3</firstname>
<lastname>Patience_surname_3</lastname>
<age>23</age>
<gender>Female</gender>
<exams>
<exam>
<id>3</id>
<date>2/29/2016 12:18:44</date>
<comment>Exam completed for patience 3</comment>
</exam>
</exams>
</patient>
</patients>
【问题讨论】:
标签: wpf xml-serialization