Search code examples
c#xmlapirestsharp

Deserializing XML Response using RestSharp


I have read through the various questions already posed here regarding this subject and i'm still no closer to solving my problem.

I am trying to Deserialize this xml response:

 <?xml version="1.0" encoding="ISO-8859-1" ?>
   <SubmissionResult>
     <Result>ACCEPTED</Result>
       <SubmissionID>
         <RefNo>77587-1425386500</RefNo>
         <Submitted>9</Submitted>
         <ValidNo>7</ValidNo>
         <InvalidNo>2</InvalidNo>
         <InvalidNo_1>08452782055</InvalidNo_1>
         <InvalidNo_2>01234567890</InvalidNo_1>
         <TextvID>77587-1425386500</TextvID>
       </SubmissionID>
     <Credits>999999</Credits>
   </SubmissionResult> 

Using these classes:

[XmlRoot ("SubmissionResult")]
public class SubmissionResult
{
    [XmlElement ("Result")]
    public string Result { get; set; }
    public SubmissionID SubmissionID { get; set; }
    [XmlElement("Credits")]
    public int Credits { get; set; }
}

public class SubmissionID
{
    [XmlElement("RefNo")]    
    public int RefNo { get; set; }
    [XmlElement("Submitted")]    
    public int Submitted { get; set; }
    [XmlElement("ValidNo")]    
    public int ValidNo { get; set; }
    [XmlElement("TextVID")]    
    public string TextVID { get; set; }
}

However I am only ever returning null and 0 values, which I assume are the default.

Here is the code to get the results and hopefully deserialize:

try
{
    var request = new RestRequest();
    request.RequestFormat = DataFormat.Xml;
    request.Resource = APIURL;
    request.RootElement = "SubmissionResult";

    SubmissionResult r = Execute<SubmissionResult>(request);
}

public static T Execute<T>(RestRequest request) where T : new()
{
     var client = new RestClient();
         client.BaseUrl = new  Uri("https://www.textvertising.co.uk/_admin/api", UriKind.Absolute);

     var response = client.Execute<T>(request);

     return response.Data;
}

Many thanks in advance for any help you can provide.

CS


Solution

  • I made a few changes and could make it work:

    Probably a typo in the question but something I had to change is that posted XML is invalid:

    <InvalidNo_2>01234567890</InvalidNo_1>
    

    I understand you want use .NET XMLSerializer as you used XmlRoot and XmlElement annotations so you have to override the default one that comes with RestSharp like so:

    request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer();
    

    Also I had to delete the RootElement setting which didn't play nicely with .NET serializer so deleted the following line:

    request.RootElement = "SubmissionResult";   
    

    So my version became:

    var request = new RestRequest();
    request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer();
    request.RequestFormat = DataFormat.Xml;
    // request.Resource = APIURL;
    
    SubmissionResult r = Execute<SubmissionResult>(request);
    

    Finally I noticed RefNo is meant to be integer in the class but the returned value is not (77587-1425386500) so I made it a string:

    [XmlElement("RefNo")]
    public string RefNo { get; set; }
    

    I created a mock response at mocky.io at tested with that and it seems to be working fine:

    public static T Execute<T>(RestRequest request) where T : new()
    {
        var client = new RestClient();
        client.BaseUrl = new Uri("http://www.mocky.io/v2/56cd88660f00009800d61ff8", UriKind.Absolute);
    
        var response = client.Execute<T>(request);
    
        return response.Data;
    }