How popular is an object with the return of a WebApi query?

2

How popular is the variable _clientes type Cliente with the return of a query to a WebApi ?

Following the great suggestion of the Damon Dudek I came across the error below:

publicclassClienteController:Controller{HttpClient_client;Uri_clienteUri;//GET:ClientepublicActionResultIndex(){if(_client==null){_client=newHttpClient();_client.BaseAddress=newUri("http://localhost:58573");
                    _client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                }
                ArrayOfCliente _clientes = Listar();
                return View(_clientes);
            }

        private ArrayOfCliente Listar()
        { 
            HttpResponseMessage response = _client.GetAsync("api/clientes").Result;
            ArrayOfCliente oPessoa = new ArrayOfCliente();

            if (response.IsSuccessStatusCode)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfCliente));

//
                using (TextReader reader = new StringReader(response))
                {
                    ArrayOfCliente result = (ArrayOfCliente)serializer.Deserialize(reader);
                }
            }
            else
            {
                Response.Write(response.StatusCode.ToString() + " - " + response.ReasonPhrase);
            }
            return oPessoa;
        }

Return from WebApi:

<ArrayOfCliente xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Dominio.Apolo.Modelo">
<Cliente>
<ClienteId>3</ClienteId>
<DtCadastro>2017-08-22T00:00:00</DtCadastro>
<Nome>Artefatos</Nome>
<RazaoSocial>Art e Fatos</RazaoSocial>
<TipoPessoa>PJ</TipoPessoa>
</Cliente>
<Cliente>
<ClienteId>4</ClienteId>
<DtCadastro>2017-08-22T00:00:00</DtCadastro>
<Nome>Empresa e Ind.</Nome>
<RazaoSocial>Nicks Oliveira</RazaoSocial>
<TipoPessoa>PJ</TipoPessoa>
</Cliente>
</ArrayOfCliente>

Model Client:

public class Cliente
    {
        public int ClienteId { get; set; }
        public string Nome { get; set; }
        public string RazaoSocial { get; set; }
        public string TipoPessoa { get; set; }
        public DateTime DtCadastro { get; set; }
    }
    
asked by anonymous 22.08.2017 / 23:37

2 answers

2

You can use C # Serialization, as per the steps below:

class import:

using System.Xml.Serialization;

Define the elements of your XML in the class:

[XmlRoot(ElementName = "ArrayOfCliente", Namespace = "http://schemas.datacontract.org/2004/07/Dominio.Apolo.Modelo")]
public class ArrayOfCliente
{
    [XmlElement("Cliente")]
    public List<cliente> cliente { get; set; }
}


public class cliente
{
    [XmlElement("ClienteId")]
    public int ClienteId { get; set; }

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

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

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

    [XmlElement("DtCadastro")]
    public DateTime DtCadastro { get; set; }
}

After this, just call the XmlSerializer from C #:

    XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfCliente));
            using (TextReader reader = new StringReader(@"<ArrayOfCliente xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/Dominio.Apolo.Modelo'>
  <Cliente>
    <ClienteId>3</ClienteId>
    <DtCadastro>2017-08-22T00:00:00</DtCadastro>
    <Nome>Artefatos</Nome>
    <RazaoSocial>Art e Fatos</RazaoSocial>
    <TipoPessoa>PJ</TipoPessoa>
  </Cliente>
  <Cliente>
    <ClienteId>4</ClienteId>
    <DtCadastro>2017-08-22T00:00:00</DtCadastro>
    <Nome>Empresa e Ind.</Nome>
    <RazaoSocial>Nicks Oliveira</RazaoSocial>
    <TipoPessoa>PJ</TipoPessoa>
  </Cliente>
</ArrayOfCliente>"))
            {
                ArrayOfCliente result = (ArrayOfCliente)serializer.Deserialize(reader);
            }
    
23.08.2017 / 00:25
1

By default the .Net in both webapi and MVC brings a package called Newtonsoft.Json, with it it is possible to deserialize json to an object and the code would look like below, but as its api is returning XML, first you would have to turn the xml into json.

NOTE: If your api can return json, your problem resolves with the following code:

private Cliente Listar()
{
    HttpResponseMessage response = _client.GetAsync("api/clientes").Result;
    Cliente _clientes = new Cliente();
    if (response.IsSuccessStatusCode)
    {
          _clientes = JsonConvert.DeserializeObject<Cliente>(response);                   
    }
    else
        Response.Write(response.StatusCode.ToString() + " - " + response.ReasonPhrase);

    return _clientes;
}
    
22.08.2017 / 23:52