Wpf xml文件中未序列化的嵌套对象
这是我用来保存序列化数据的模型。。当我运行主属性(名称、姓氏..)时,从xml设置为罚款,但在嵌套对象(检查)中,属性(id、日期、注释)在其中为空 代码中的什么给出了这个问题Wpf xml文件中未序列化的嵌套对象,wpf,xml-serialization,Wpf,Xml Serialization,这是我用来保存序列化数据的模型。。当我运行主属性(名称、姓氏..)时,从xml设置为罚款,但在嵌套对象(检查)中,属性(id、日期、注释)在其中为空 代码中的什么给出了这个问题 namespace WpfApplication1 { [Serializable, XmlRoot("patients")] public class patients { [XmlElement("patient")] public List<patien
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; }
}
}
和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>
耐心(姓名)1
你姓什么
20
男性
1.
2/29/2016 12:18:44
患者1的考试已完成
耐心(姓名)2
你姓什么
22
男性
2.
2/29/2016 12:18:44
考试完成时间为2分钟
耐心(姓名)3
你姓什么
23
女性
3.
2/29/2016 12:18:44
耐心3的考试已经完成
您有以下问题:
检查
列表中有一个外部容器元素
,每个项目都有一个内部元素
:
<exams>
<exam>
<!-- First exam data -->
</exam>
<exam>
<!-- Second exam data if present -->
</exam>
<exams>
要捕获这一点,您需要添加一个额外的类[可序列化,XmlRoot(“患者”)]
公营病人
{
[XmlElement(“患者”)]
公共列表患者\u列表{get;set;}
}
公立病人
{
[XmlElement(“名字”)]
公共字符串名称{get;set;}
[XmlElement(“lastname”)]
公共字符串姓氏{get;set;}
[XmlElement(“年龄”)]
公共整数{get;set;}
公共字符串{get;set;}
[XmlArray(“考试”)]
[XmlArrayItem(“考试”)]
公共列表考试{get;set;}
}
公开课考试
{
[XmlElement(“id”)]
公共int id{get;set;}
[XmlIgnore]
公共日期时间日期{get;set;}
[XmlElement(“日期”)]
公共字符串日期字符串
{
得到
{
返回XmlConvert.ToString(日期,XmlDateTimeSerializationMode.Utc);
}
设置
{
//您需要检查XML文件中的日期和时间是通用时间还是本地时间!
date=DateTime.Parse(值,CultureInfo.InvariantCulture,DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
}
}
[XmlElement(“注释”)]
公共注释注释{get;set;}
}
公开课评论
{
[XmlAttribute(“值”)]
公共字符串值{get;set;}
[XmlText]
公共字符串CommentData{get;set;}
}
您有以下问题:
检查
列表中有一个外部容器元素
,每个项目都有一个内部元素
:
<exams>
<exam>
<!-- First exam data -->
</exam>
<exam>
<!-- Second exam data if present -->
</exam>
<exams>
要捕获这一点,您需要添加一个额外的类[可序列化,XmlRoot(“患者”)]
公营病人
{
[XmlElement(“患者”)]
公共列表患者\u列表{get;set;}
}
公立病人
{
[XmlElement(“名字”)]
公共字符串名称{get;set;}
[XmlElement(“lastname”)]
公共字符串姓氏{get;set;}
[XmlElement(“年龄”)]
公共整数{get;set;}
公共字符串{get;set;}
[XmlArray(“考试”)]
[XmlArrayItem(“考试”)]
公共列表考试{get;set;}
}
公开课考试
{
[XmlElement(“id”)]
公共int id{get;set;}
[XmlIgnore]
公共日期时间日期{get;set;}
[XmlElement(“日期”)]
公共字符串日期字符串
{
得到
{
返回XmlConvert.ToString(日期,XmlDateTimeSerializationMode.Utc);
}
设置
{
//您需要检查XML文件中的日期和时间是通用时间还是本地时间!
date=DateTime.Parse(值,CultureInfo.InvariantCulture,DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
}
}
[XmlElement(“注释”)]
公共注释注释{get;set;}
}
公开课评论
{
[XmlAttribute(“值”)]
公共字符串值{get;set;}
[XmlText]
公共字符串CommentData{get;set;}
}
嗨,你能看看这篇文章吗。谢谢你好,你能查一下这个帖子吗。非常感谢。
<comment value="patiente">Exam completed for patience1</comment>
[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; }
[XmlArray("exams")]
[XmlArrayItem("exam")]
public List<exam> exam { get; set; }
}
public class exam
{
[XmlElement("id")]
public int id { get; set; }
[XmlIgnore]
public DateTime date { get; set; }
[XmlElement("date")]
public string DateString
{
get
{
return XmlConvert.ToString(date, XmlDateTimeSerializationMode.Utc);
}
set
{
// You will need to check whether the dates and times in your XML file are in universal time or local time!
date = DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
}
}
[XmlElement("comment")]
public Comment comment { get; set; }
}
public class Comment
{
[XmlAttribute("value")]
public string Value { get; set; }
[XmlText]
public string CommentData { get; set; }
}