0

I send a RestRequest and receive the response in xml format. This is my code:

class Employee 
{
    int id;
    string name;
}
private void reqEmployee(Employee empl)
{
    var client = new RestClient(ConfigurationManager.AppSettings["base_url"] + "/" +
                                ConfigurationManager.AppSettings["provider"] + "/" +
                                ConfigurationManager.AppSettings["serviceName"])
    {
        Timeout = -1
    };

    var request = new RestRequest(Method.POST);

    var authenticationString = string.Format("{0}:{1}", ConfigurationManager.AppSettings["serviceUser"], ConfigurationManager.AppSettings["servicePass"]);
    var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));

    request.AddHeader("Authorization", "Basic " + base64EncodedAuthenticationString);
    request.AddHeader("Content-Type", "text/plain");

    var body = @"<GetEmployee xmlns=""http://test.dev.com/"">"
                + "\n" + @"    <Id>" + empl.idno + "</Id>"
                + "\n" + @"</GetEmployee>";


    request.AddParameter("text/plain", body, ParameterType.RequestBody);

    IRestResponse response = client.Execute(request);
}

Example of the request I send:

<GetEmployee xmlns="http://test.dev.com/">
    <Id>4132</Id>   
</GetEmployee>

Example of the response I receive (response.Content):

<GetEmployeeResponse xmlns="http://test.dev.com/">
    <PropertyStatus xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Employee>
            <Name>John</Name>
            <Id>6584</Id>
        </Employee>
        <Employee>
            <Name>Chris</Name>
            <Id>4120</Id>
        </Employee>
    </PropertyStatus>
</GetEmployeeResponse>

How can I fill a List<Employee> with response.Content?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Chriz
  • 123
  • 12
  • Does this answer your question? [Building XML request with RestSharp](https://stackoverflow.com/questions/34527935/building-xml-request-with-restsharp) – Peter Csala Aug 30 '21 at 11:31
  • Thank you for the asnwer, this link is about sending a request with an array of objects, I just want to read the response and save it as an array of employee – Chriz Aug 30 '21 at 11:55
  • If you set the DataFormat and Serializer on the request then you can call the `Execute` where you can specify `List` as the expected output. – Peter Csala Aug 30 '21 at 12:24
  • Here is [another SO thread](https://stackoverflow.com/questions/35598796/deserializing-xml-response-using-restsharp) where this concept is detailed. – Peter Csala Aug 30 '21 at 12:25

1 Answers1

0

You just need to:

  • configure the default XML serializer
  • prepare your POCO class to be serializable with serialization attributes
  • use RestSharp Client generic Execute<T>

https://restsharp.dev/usage/serialization.html#default-serializers

https://restsharp.dev/getting-started/getting-started.html#content-type

Hermes Monteiro
  • 101
  • 1
  • 3