0

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 i.e.:

<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 i.e. 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

LAWman
  • 1
  • 2
  • I expect your class needs the attribute `[XmlElement("Education")]` as well and then I hope the List will map to an ArrayOf of that class. And none of your other elements map to the properties of your class. – rene Apr 15 '17 at 18:39
  • Here is the duplicate explaining the root cause http://stackoverflow.com/a/1557145/920557 – Eugene Komisarenko Apr 15 '17 at 18:41
  • 2
    Possible duplicate of [{" was not expected.} Deserializing Twitter XML](http://stackoverflow.com/questions/1556874/user-xmlns-was-not-expected-deserializing-twitter-xml) – rene Apr 15 '17 at 18:42

1 Answers1

0

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; }
 }
Avijit
  • 1,219
  • 2
  • 15
  • 28
  • Hi Avijit. Thanks for taking the time out to comment. However what happens when i'm calling the web service and just getting a singular Education item back (non array): - surely I should just be able to use one simple Education class to deal with both cases? – LAWman Apr 15 '17 at 21:18