【问题标题】:Error when deserializing xml into array将 xml 反序列化为数组时出错
【发布时间】:2017-04-15 18:17:31
【问题描述】:

我正在尝试反序列化从 Web 服务调用中检索到的 xml

using (var client = new WebClient())
{
    client.UseDefaultCredentials = true;
    var content = client.DownloadString("call to service");
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<NewCourseApply.Models.Education>));
    using (TextReader textReader = new StringReader(content))
    {
        var e = (List<NewCourseApply.Models.Education>)serializer.Deserialize(textReader);
    }
}

服务返回的xml是:

<ArrayOfEducation xmlns="http://schemas.datacontract.org/2004/07/CovUni.Domain.Admissions" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Education><_auditList xmlns="http://schemas.datacontract.org/2004/07/CovUni.Common.Base" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/><_apCode>670104552</_apCode><_attendanceType>FT</_attendanceType><_educationId>1</_educationId><_establishmentDetails>test school</_establishmentDetails><_fromDate>2016-11-01T00:00:00</_fromDate><_toDate>2016-11-22T00:00:00</_toDate><_ucasSchoolCode/></Education></ArrayOfEducation>

我的客户端对象是:

[Serializable]
public class Education
{
    protected int  _apCode;
    protected int  _educationId;
    protected string  _establishmentDetails;
    protected string  _ucasSchoolCode;
    protected DateTime?  _fromDate;
    protected DateTime? _toDate;
    protected string  _attendanceType;
    protected string  _auditList;

    public int  ApCode
    { get { return _apCode;}
      set { _apCode = value;} }

    public int  EducationId
    { get { return _educationId;}
      set { _educationId = value;} }

    public string  EstablishmentDetails
    { get { return _establishmentDetails;}
      set { _establishmentDetails = value;} }

    public string  UcasSchoolCode
    { get { return _ucasSchoolCode;}
      set { _ucasSchoolCode = value;} }

    public DateTime?  FromDate
    { get { return _fromDate;}
      set { _fromDate = value;} }

    public DateTime?  ToDate
    { get { return _toDate;}
      set { _toDate = value;} }

    public string  AttendanceType
    { get { return _attendanceType;}
      set { _attendanceType = value;} }

    public string  AuditList
    { get { return _auditList;}
      set { _auditList = value;} }

}

我得到的错误是:

XML 文档 (1, 2) 中存在错误。

<ArrayOfEducation xmlns='http://schemas.datacontract.org/2004/07/CovUni.Domain.Admissions'> was not expected.

另外,如果我调用网络服务并获得单一的教育响应,即:

<Education xmlns="http://schemas.datacontract.org/2004/07/CovUni.Domain.Admissions" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><_auditList xmlns="http://schemas.datacontract.org/2004/07/CovUni.Common.Base" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/><_apCode>670104552</_apCode><_attendanceType>FT</_attendanceType><_educationId>1</_educationId><_establishmentDetails>test school</_establishmentDetails><_fromDate>2016-11-01T00:00:00</_fromDate><_toDate>2016-11-22T00:00:00</_toDate><_ucasSchoolCode/></Education>

当然,我只需要客户端的一个简单教育类,它可以从我提供的 2 个 xml 示例(即数组和非数组)反序列化

各位好心人可以告诉我哪里出错了,或者是否有更好的方法可以做到这一点?

非常感谢

【问题讨论】:

标签: c# xml


【解决方案1】:

将类更改为

[XmlRoot("ArrayOfEducation", Namespace = "http://schemas.datacontract.org/2004/07/CovUni.Domain.Admissions")]
public class ArrayOfEducation
{
    [XmlElement("Education")]
    public List<ContainerEducation> education { get; set; }
}
public class ContainerEducation
{
    [XmlElement(ElementName = "_apCode")]
    public int _apCode { get; set; }
    [XmlElement(ElementName = "_educationId")]
    public int _educationId { get; set; }
    [XmlElement(ElementName = "_establishmentDetails")]
    public string _establishmentDetails { get; set; }
    [XmlElement(ElementName = "_ucasSchoolCode")]
    public string _ucasSchoolCode { get; set; }
    [XmlElement(ElementName = "_fromDate")]
    public DateTime? _fromDate { get; set; }
    [XmlElement(ElementName = "_toDate")]
    public DateTime? _toDate { get; set; }
    [XmlElement(ElementName = "_attendanceType")]
    public string _attendanceType { get; set; }
    [XmlElement(ElementName = "_auditList", Namespace = "http://schemas.datacontract.org/2004/07/CovUni.Common.Base")]
    public string _auditList { get; set; }
}

并以下面的方式反序列化。现在,当我运行代码来反序列化您的 XML 时,我确实很好地填充了对象。

XmlSerializer mySerializer = new XmlSerializer(typeof(ArrayOfEducation));
using (TextReader textReader = new StringReader(content))
{
   ArrayOfEducation arrEdu = (ArrayOfEducation)mySerializer.Deserialize(textReader);
}

根据您的评论更新: 如果您确定网络服务将发送单个教育,那么您需要将类更改为

[XmlRoot("ArrayOfEducation", Namespace = "http://schemas.datacontract.org/2004/07/CovUni.Domain.Admissions")]
 public class ArrayOfEducation
 {
  [XmlElement("Education")]
  public ContainerEducation education { get; set; }
 }

【讨论】:

  • 嗨阿维吉特。感谢您抽出时间发表评论。但是,当我调用 Web 服务并返回一个单一的 Education 项目(非数组)时会发生什么:-当然我应该能够使用一个简单的 Education 类来处理这两种情况?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
相关资源
最近更新 更多