将xml反序列化为数组时出错

时间:2017-04-15 18:17:32

标签: c# xml

我正在尝试反序列化从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>

当然我只需要客户端上的一个简单教育类,可以从我提供的xml的两个例子中反序列化,即数组和非数组

你们有些善良的灵魂能让我知道我哪里出错了,还是有更好的办法呢?

非常感谢

1 个答案:

答案 0 :(得分:0)

将班级更改为

[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);
}

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

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