xml 文件中未序列化的嵌套对象

Nested objects not serialized in xml file

这是我用来保存序列化数据的模型。当我 运行 主要属性(姓名、姓氏..)从 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>

您遇到以下问题:

  1. 您的 exams 列表有一个外部容器元素 <exams>,每个项目都有一个内部元素 <exam>

    <exams>
      <exam>
        <!-- First exam data -->
      </exam>
      <exam>
        <!-- Second exam data if present -->
      </exam>
    <exams>
    

    要序列化带有外部容器元素的列表,请使用 XmlArray and XmlArrayItem

  2. 您的日期字符串不正确 ISO 8601 formatXmlSerializer 将在尝试反序列化非此格式的日期和时间字符串时抛出异常。为了更加宽容,您需要创建一个字符串值代理 属性 来反序列化日期。

    由于您的日期字符串不是 ISO 8601 格式,因此它不包含时区信息。您需要从 XML 的提供者那里确定日期字符串的时区! (或者最好让他们以正确的格式生成日期和时间。)

  3. 您的 <comment> 元素有一个属性 "value" 您没有反序列化:

    <comment value="patiente">Exam completed for patience1</comment>
    

    要捕获此内容,您需要添加额外的 class.

因此:

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