crocodile_smiles
crocodile_smiles

Reputation: 113

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

Upvotes: 11

Views: 20893

Answers (3)

KR Akhil
KR Akhil

Reputation: 1017

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

Upvotes: 5

Volkan Paksoy
Volkan Paksoy

Reputation: 6957

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

Upvotes: 5

DaveHutchy
DaveHutchy

Reputation: 82

Using Restsharp i normally do a different approach but i am mainly working with JSON. Is this using MVC web api?

I would create a Client being of Type IRestClient where i would set the API url and some headers. I would then use,

var response = client.Post<T>(DataFormat.Json, "admin/api", content); 
if(response.Data != null)
//populate T object with data

If you could please provide some more information on the web service method you are calling.. it would help thanks :) good luck with this..

Have a look at this http://restsharp.org/ To be honest i have never used the RestClient.Execute<> myself but this link should hopefully help

Upvotes: -3

Related Questions