无法将 XML 字符串反序列化为包含列表的对象
Cannot deserialize XML string to object containing List
这是我的序列化方法:
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<> 属性。
我在这里做错了什么或遗漏了什么?
实际上,当我使用 XML 和上面的示例代码时,它并没有为我填充任何字段。我不完全清楚你是直接将海水淡化到 Patient 中,但是当我添加一个 PatientDS class 时,它是 XML 中的根,它填充了所有字段,包括所有列表。我刚刚添加了这一点:
public class PatientDS
{
[XmlElement("Patient")]
public Patient Patient { get; set; }
}
我这样反序列化:
string xml = File.ReadAllText("XMLFile1.xml");
var patient = DeserializeFromXml<PatientDS>(xml);
并且输出看起来不错:
希望这就是您要找的。
试试这个:
[XmlRoot(ElementName = "PatientDS")]
public class PatientDS
{
public Patient Patient { get; set; }
}
[XmlRoot(ElementName = "Patient")]
public class Patient
{
//stuff
}
并反序列化它。
我发现了问题。
在阅读了有关 XML 中读取日期的更多信息后,我认为此 XML 的子项中提供的日期无效(ISO 8601)标准,因此我的应用程序遇到了困难阅读它,但失败了。
因为这是一个虚拟对象 XML,我实例化了一个新的 PersonDS 对象,将其反序列化为 XML,然后反序列化以检查应用程序是否正常工作,并且它没有序列化问题。
感谢大家的帮助。
这是我的序列化方法:
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<> 属性。
我在这里做错了什么或遗漏了什么?
实际上,当我使用 XML 和上面的示例代码时,它并没有为我填充任何字段。我不完全清楚你是直接将海水淡化到 Patient 中,但是当我添加一个 PatientDS class 时,它是 XML 中的根,它填充了所有字段,包括所有列表。我刚刚添加了这一点:
public class PatientDS
{
[XmlElement("Patient")]
public Patient Patient { get; set; }
}
我这样反序列化:
string xml = File.ReadAllText("XMLFile1.xml");
var patient = DeserializeFromXml<PatientDS>(xml);
并且输出看起来不错:
希望这就是您要找的。
试试这个:
[XmlRoot(ElementName = "PatientDS")]
public class PatientDS
{
public Patient Patient { get; set; }
}
[XmlRoot(ElementName = "Patient")]
public class Patient
{
//stuff
}
并反序列化它。
我发现了问题。
在阅读了有关 XML 中读取日期的更多信息后,我认为此 XML 的子项中提供的日期无效(ISO 8601)标准,因此我的应用程序遇到了困难阅读它,但失败了。
因为这是一个虚拟对象 XML,我实例化了一个新的 PersonDS 对象,将其反序列化为 XML,然后反序列化以检查应用程序是否正常工作,并且它没有序列化问题。
感谢大家的帮助。