2016-02-29 101 views
1

這裏是我用來保存序列化數據的模型。當我運行主要屬性(名稱,姓氏)從xml中設置罰款,但殭屍嵌套對象(考試)的屬性(ID ,日期,評論)在它內部爲空未在xml文件中序列化的嵌套對象

代碼中的什麼讓這個問題出現?

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> 

回答

0

您有以下問題:

  1. exams列表有一個外部聯繫INER元件<exams>與內部元件<exam>每個項目:

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

    要序列的列表,包括一個外容器元件,使用XmlArrayXmlArrayItem

  2. 您的日期字符串不正確ISO 8601 formatXmlSerializer會拋出一個異常,試圖反序列化不是這種格式的日期和時間字符串。爲了更寬容,您需要創建一個字符串值的代理屬性來反序列化日期。

    由於您的日期字符串不是ISO 8601格式,因此它不包含時區信息。您將需要從XML的提供者確定日期字符串的時區! (或者更好的是讓他們產生正確格式的日期和時間。)

  3. <comment>元素具有屬性"value"你是不是反序列化:

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

你是傳說!你是英雄!你是男人! – TyForHelpDude

+0

嗨,你可以檢查這個職位。 http://stackoverflow.com/questions/35732183/adding-objects-to-treeview-as-expanded謝謝 – TyForHelpDude