【问题标题】:How to deserialize an .XML into a list如何将 .XML 反序列化为列表
【发布时间】:2014-09-20 07:55:56
【问题描述】:

假设我有一个包含患者信息的 XML 文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPatient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Patient>
    <firstName>Vince</firstName>
    <lastName>Smith</lastName>
    <dateOfBirth>05/05/1912</dateOfBirth>
    <phone>3056988877</phone>
    <email>random@google.com</email>
    <insurance>Humana</insurance>
    <typeOfPlan>PPA</typeOfPlan>
    <subID>123456</subID>
    <planID>654321</planID>
  </Patient>
  <Patient>
    <firstName>Mark</firstName>
    <lastName>Jones</lastName>
    <dateOfBirth>05/05/1992</dateOfBirth>
    <phone>3058877457</phone>
    <email>random@hotmail.com</email>
    <insurance>PlanB</insurance>
    <typeOfPlan>PPO</typeOfPlan>
    <subID>987987</subID>
    <planID>987987</planID>
  </Patient>

如何将该信息加载到 Visual Studios 中的患者列表中?这是我的尝试,但出现错误:

Dim Patients As New List(Of Patient)
Dim p As New Patient
Dim reader As New StreamReader("..\..\patients.xml")
Dim serial As New XmlSerializer(GetType(List(Of Patient)))
For Each paciente In serial.Deserialize(reader)
    Patients.Add(paciente)
Next

但是它给我一个未处理的表达式错误。

患者类仅具有 XML 中所有字段的所有属性。

【问题讨论】:

  • 异常的信息是什么?
  • 并且:除了完整的消息文本:这是编译器错误吗?还是运行时错误?如果是运行时:确保您还包括内部异常的消息 - 一直到内部异常为空。

标签: .net deserialization xml-deserialization


【解决方案1】:

应该只是一个铸造的东西。我的 VB 生锈了,但在 C# 中:

List<Patient> patients = (List<Patient>)serial.Deserialize(reader);

序列化程序应该已经返回了一个列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多