简体   繁体   中英

Error when deserializing xml into array

I'm trying to deserialize xml retrieved from a web service call

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

The xml returned from the service is:

<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>

My client side object is:

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

}

The error I am getting is:

There is an error in XML document (1, 2).

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

Also if I call a web service call and get the singular Education response ie:

<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>

Surely I just need one Simple Education class on the client side that can deserialise from the 2 examples of xml i have provided ie array and non array

Can some of you kind souls let me know where i'm going wrong or if there's a better way of doing this?

Many Thanks

Change the Class to

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

And Deserialize below way. Now, when I run the code to deserialize your XML, I do get the objects filled nicely.

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

Update as per your comment: If you are sure that web service is going to send single Education then you need to change the class to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM