Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 364
crocodile_smiles Avatar asked Feb 24 '16 09:02

crocodile_smiles


2 Answers

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;
}
like image 123
Volkan Paksoy Avatar answered Oct 06 '22 18:10

Volkan Paksoy


Simple code to De-serialize xml response

//Create object of RestClient
IRestClient restClient = new RestClient();

//Create request with return type of payload as xml and method as GET
IRestRequest restRequest = new RestRequest(Url);
restRequest.AddHeader("Accept", "application/xml");
restRequest.Method = Method.GET;

//Execute the request to fetch the response
IRestResponse restResponse = restClient.Execute(restRequest);

//This is wrapper for System.Xml.Serialization.XmlSerializer 
var dotNetXmlDeserializer = new RestSharp.Deserializers.DotNetXmlDeserializer();
ModelClassName modelClassObject =
 dotNetXmlDeserializer.Deserialize<ModelClassName>(restResponse);

 Console.WriteLine(ModelClassName.PropertyName);

       

Where ModelClassName is the POCO class we create to store the response in de-serialized form.

You can paste the copied payload in JSON/XML format into POCO class by using Visual Studios in built feature. Edit -> Paste Special -> Paste JSON/XML as classes

like image 23
KR Akhil Avatar answered Oct 06 '22 18:10

KR Akhil