2

Here is my model I use to keep serialized data.. When I run main properties (name, surname..) set fine from xml but bot the nested objects (exam) the attributes (id, date, comment) are null inside of it

What in the code gives way this problem?

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

and my main code that makes serializing:

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

and the xml file:

<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>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TyForHelpDude
  • 4,828
  • 10
  • 48
  • 96

1 Answers1

0

You have the following problems:

  1. Your exams list has an outer container element <exams> with inner elements <exam> for each item:

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

    To serialize a list with an outer container element, use XmlArray and XmlArrayItem.

  2. Your date string is not in proper ISO 8601 format. XmlSerializer will throw an exception trying to deserialize a date and time string not in this format. To be more forgiving, you will need to create a string-valued proxy property to deserialize the date.

    Since your date string is not in ISO 8601 format, it does not contain time zone information. You will need to determine from the provider of the XML the time zone for the date string! (Or better yet have them generate the date and time in proper format.)

  3. Your <comment> element has an attribute "value" you are not deserializing:

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

    To capture this you will need to add an extra class.

Thus:

[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; }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
  • Hi, can you check this post. http://stackoverflow.com/questions/35732183/adding-objects-to-treeview-as-expanded Thank you – TyForHelpDude Mar 01 '16 at 22:13