none
Remove Response Tag RRS feed

  • Question

  • Hi,

    I have a RESTful WCF service that have to return the following XML:

    <?xml version="1.0" encoding="utf-8"?>
    <Abastecimentos>
       <Abastecimento>
          <Empresa>01</Empresa>
       </Abastecimento>
       <Abastecimento>
          <Empresa>01</Empresa>
       </Abastecimento>
       <Abastecimento>
          <Empresa>01</Empresa>
       </Abastecimento>
    </Abastecimentos>

    But it is returning this:

    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
    <?xml version="1.0" encoding="utf-8"?>
    <Abastecimentos>
       <Abastecimento>
          <Empresa>01</Empresa>
       </Abastecimento>
       <Abastecimento>
          <Empresa>01</Empresa>
       </Abastecimento>
       <Abastecimento>
          <Empresa>01</Empresa>
       </Abastecimento>
    </Abastecimentos>
    </string>

    My IService is as follows:

    [ServiceContract]
    public interface ITransacaoService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetTransacoes")]
        string GetTransacoes(Stream xmlEntrada);
    }

    Does anybody knows how can I remove that "string" tag from the response?

    Thank you.

    Wednesday, February 12, 2014 3:15 PM

Answers

  • Instead of a List, implement a custom collection derived from List<Abastecimento> and apply a CollectionDataContractAttribute, like so:

    [CollectionDataContractAttribute]
    public class AbastecimentoList : List<Abastecimento> { }
    Wednesday, February 12, 2014 8:00 PM

All replies

  • Hi,

    By what I've seen, the "GetTransacoes" method is returning "pure XML" as its string content, instead make it return an Array or List of Abastecimento.

    Also, as a side note, if you're going for RESTful API, when GETting stuff, use GET instead of POST.

    Best regards,
    Fernando Nunes

    Wednesday, February 12, 2014 4:17 PM
  • Hi Fernando,

    I chanced my service to return an List of "Abastecimento", but now I have another situation, instead of the "string" tag I have an "ArrayOfAbastecimento" tag, as you can see below.

    <ArrayOfAbastecimento xmlns="http://schemas.datacontract.org/2004/07/Retorno" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Abastecimento>...</Abastecimento>
    <Abastecimento>...</Abastecimento>
    <Abastecimento>...</Abastecimento>
    <Abastecimento>...</Abastecimento>
    </ArrayOfAbastecimento>

    How can I turn it to a "Abastecimentos" tag?

    Thank you very much.

    Wednesday, February 12, 2014 6:14 PM
  • Instead of a List, implement a custom collection derived from List<Abastecimento> and apply a CollectionDataContractAttribute, like so:

    [CollectionDataContractAttribute]
    public class AbastecimentoList : List<Abastecimento> { }
    Wednesday, February 12, 2014 8:00 PM
  • Hi Fernando,

    Thank you for your response, it worked just fine!

    P.S.: I agree with what you said about using GET instead of POST, but I'm just adapting my WS to an existing integration.


    Thursday, February 13, 2014 11:40 AM