无法将XML字符串反序列化为包含List的对象

时间:2015-08-19 10:00:40

标签: c# .net xml deserialization

这是我的序列化方法:

public static T DeserializeFromXml<T>(string xml)
{
    try
    {
         var root = new XmlRootAttribute()
         {
             ElementName = "PatientDS",
             Namespace = ""
         };

         T result;
         var serializer = new XmlSerializer(typeof(T), root);
         using (TextReader reader = new StringReader(xml))
         {
            result = (T)serializer.Deserialize(reader);
         }
         return result;
    }
    catch (Exception ex)
    {
        return default(T);
    }
}

public static string Serialize<T>(T obj)
{
    try
    {
       var serializer = new XmlSerializer(typeof(T));
       using (var writer = new StringWriter())
       {
           serializer.Serialize(writer, obj);
           return writer.ToString();
       }
    }
    catch (Exception ex)
    {
        return String.Empty;
    }
}

这是我试图反序列化的对象:

public class PatientDS
{
    [XmlElement("Patient")]
    public Patient Patient { get; set; }
}

    [Serializable]
    public class Patient
    {
        [XmlElement("PatientID")]
        public Guid PatientID { get; set; }

        [XmlElement("Gender")]
        public string Gender { get; set; }

        [XmlElement("PatientCategory")]
        public string PatientCategory { get; set; }

        [XmlElement("SiteOfDisease")]
        public string SiteOfDisease { get; set; }

        [XmlElement("Outcome")]
        public string Outcome { get; set; }

        [XmlElement("RegistrationYear")]
        public int RegistrationYear { get; set; }

        [XmlElement("RegistrationNo")]
        public int RegistrationNo { get; set; }

        [XmlElement("RegistrationDate")]
        public DateTime RegistrationDate { get; set; }

        [XmlElement("Name")]
        public string Name { get; set; }

        [XmlElement("Surname")]
        public string Surname { get; set; }

        [XmlElement("IDNumber")]
        public string IDNumber { get; set; }

        [XmlElement("BirthDate")]
        public DateTime BirthDate { get; set; }

        [XmlElement("TypeOfResistantTB")]
        public string TypeOfResistantTB { get; set; }

        [XmlElement("TreatmentStartDate")]
        public DateTime TreatmentStartDate { get; set; }

        [XmlElement("OutcomeDate")]
        public DateTime OutcomeDate { get; set; }

        [XmlElement("IsActive")]
        public bool IsActive { get; set; }

        [XmlElement("IsTreatmentStarted")]
        public bool IsTreatmentStarted { get; set; }

        [XmlElement("TypeOfResistantTBConfirmation")]
        public string TypeOfResistantTBConfirmation { get; set; }

        [XmlElement("HIVStatus")]
        public string HIVStatus { get; set; }

        [XmlArray("PatientDSTDS")]
        [XmlArrayItem("PatientDST")]
        public List<PatientDST> PatientDSTDS { get; set; }

        [XmlElement("PatientHIVTestDS")]
        public List<PatientHIVTest> PatientHIVTestDS { get; set; }

        [XmlElement("PatientDSTAdvancedDS")]
        public List<PatientDSTAdvanced> PatientDSTAdvancedDS { get; set; }

        [XmlElement("PatientSecondLineDrugDS")]
        public List<PatientSecondLineDrug> PatientSecondLineDrugDS { get; set; }

        [XmlElement("PatientSputumDS")]
        public List<PatientSputum> PatientSputumDS { get; set; }
    }

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<PatientDS>
  <Patient>
    <PatientID>ED526A85-26E3-424A-B15C-AB32FD114F5E</PatientID>
    <Gender>F</Gender>
    <PatientCategory>N</PatientCategory>
    <SiteOfDisease>PTB</SiteOfDisease>
    <Outcome></Outcome>
    <RegistrationYear>2010</RegistrationYear>
    <RegistrationNo>100</RegistrationNo>
    <RegistrationDate>2010-06-21</RegistrationDate>
    <Name>Jane</Name>
    <Surname>Doe</Surname>
    <IDNumber>7001010099080</IDNumber>
    <BirthDate>1970-01-01</BirthDate>
    <TypeOfResistantTB>MDR</TypeOfResistantTB>
    <TreatmentStartDate>2010-06-21</TreatmentStartDate>
    <OutcomeDate>2010-11-30</OutcomeDate>
    <IsActive>1</IsActive>
    <IsTreatmentStarted>1</IsTreatmentStarted>
    <TypeOfResistantTBConfirmation>LABCONFIRMED</TypeOfResistantTBConfirmation>
    <HIVStatus>P</HIVStatus>
    <PatientDSTDS>
     <PatientDST>
      <TestDate>2010-06-22 08:23:00.000</TestDate>
      <ReferenceNo>NNY0288029</ReferenceNo>
      <PatientDSTDrugDS>
        <PatientDSTDrug>
          <DSTDrug>R</DSTDrug>
          <DSTResult>R</DSTResult>
        </PatientDSTDrug>
        <PatientDSTDrug>
          <DSTDrug>Eto</DSTDrug>
          <DSTResult>S</DSTResult>
        </PatientDSTDrug>
     </PatientDSTDrugDS>
  </PatientDST>
  <PatientDST>
    <PatientDSTID>FBFA09A9-452B-4BA2-BC01-81297EFCC8FD</PatientDSTID>
    <TestDate>2011-07-11 15:50:49.000</TestDate>
    <ReferenceNo>NTH0168507</ReferenceNo>
    <PatientDSTDrugDS>
      <PatientDSTDrug>
        <DSTDrug>R</DSTDrug>
        <DSTResult>R</DSTResult>
      </PatientDSTDrug>
      <PatientDSTDrug>
        <DSTDrug>Eto</DSTDrug>
        <DSTResult>S</DSTResult>
      </PatientDSTDrug>
   </PatientDSTDrugDS>
</PatientDST>
</PatientDSTDS>
<PatientHIVTestDS>
  <PatientHIVTest>
    <HIVTestResult>N</HIVTestResult>
    <TestDate>2010-02-01 00:00:00.000</TestDate>
    <CD4Count>350</CD4Count>
  </PatientHIVTest>
  <PatientHIVTest>
    <HIVTestResult>P</HIVTestResult>
    <TestDate>2010-09-25 00:00:00.000</TestDate>
    <CD4Count>280</CD4Count>
  </PatientHIVTest>
</PatientHIVTestDS>
<PatientDSTAdvancedDS>
  <PatientDSTAdvanced>
    <DSTDrugResult>R</DSTDrugResult>
    <DSTGeneXpertResult>Positive</DSTGeneXpertResult>
    <TestDate>2010-07-11 16:30:00.000</TestDate>
    <ReferenceNo>ABCD9928</ReferenceNo>
  </PatientDSTAdvanced>
  <PatientDSTAdvanced>
    <DSTDrugResult>N</DSTDrugResult>
    <DSTGeneXpertResult>Negative</DSTGeneXpertResult>
    <TestDate>2011-01-18 16:30:00.000</TestDate>
    <ReferenceNo>WXYZ9876</ReferenceNo>
  </PatientDSTAdvanced>
</PatientDSTAdvancedDS>
<PatientSecondLineDrugDS>
  <PatientSecondLineDrug>
    <SecondLineDrug>Ofx</SecondLineDrug>
    <StartDate>2010-09-25 00:00:00.000</StartDate>
  </PatientSecondLineDrug>
  <PatientSecondLineDrug>
    <SecondLineDrug>Bdq</SecondLineDrug>
    <StartDate>2010-10-13 00:00:00.000</StartDate>
  </PatientSecondLineDrug>
</PatientSecondLineDrugDS>
<PatientSputumDS>
  <PatientSputum>
    <SputumResult>R</SputumResult>
    <CultureResult>N</CultureResult>
    <TestDate>2010-06-22 00:00:00.000</TestDate>
    <ReferenceNo>ABC0168503</ReferenceNo>
    <TreatmentMonth>1</TreatmentMonth>
  </PatientSputum>
  <PatientSputum>
    <SputumResult>N</SputumResult>
    <CultureResult>P</CultureResult>
    <TestDate>2010-08-17 00:00:00.000</TestDate>
    <ReferenceNo>XYZ0358503</ReferenceNo>
    <TreatmentMonth>2</TreatmentMonth>
  </PatientSputum>
</PatientSputumDS>

问题是我能够反序列化这个对象,并且所有普通元素都填充了正确的值,但我无法得到List&lt;&gt;要填充的属性。

我在这里做错了什么或错过了什么?

3 个答案:

答案 0 :(得分:0)

实际上,当我使用上面的XML和示例代码时,它并没有为我填充任何字段。我并不完全清楚您是否已直接对患者进行脱盐,但当我添加一个PatientDS类时,它是XML的根,它填充了所有字段,包括所有列表。我刚刚补充了这一点:

public class PatientDS
{
    [XmlElement("Patient")]
    public Patient Patient { get; set; }
}

我这样反序列化:

string xml = File.ReadAllText("XMLFile1.xml");
var patient = DeserializeFromXml<PatientDS>(xml); 

并且输出似乎很好:

enter image description here

希望这就是你要找的东西。

答案 1 :(得分:0)

试试这个:

[XmlRoot(ElementName = "PatientDS")]
public class PatientDS
{
   public Patient Patient { get; set; }
}

[XmlRoot(ElementName = "Patient")]
public class Patient
{
   //stuff
}

将其反序列化。

答案 2 :(得分:0)

我发现了问题。

在阅读了有关从XML读取日期的更多信息之后,我认为此XML子项中提供的日期无效(ISO 8601)标准,因此我的应用程序正在努力阅读它并失败。

由于这是一个虚拟XML,我实例化了一个新的PersonDS对象,将其反序列化为XML,然后序列化以检查应用程序是否正常工作,并且序列化没有问题。

感谢所有人的帮助。