C# - XML return from Webservice with encoding error (ISO-8859-1)

Asked

Viewed 3,274 times

5

I have a Windows Forms application that is consuming a Webservice that returns an xml (string) enconding with ISO-8859-1.

However, the answer comes with question characters (?) instead of accents.

How to convert or read to ISO-8859-1, so that the accent comes correctly?

Code:

        string empresa = "loginEmpresa";
        string token = "tZTMMnOO+oZZmlwhSRuFbQ=="; 
        string xmlPublicacoes = "";

        br.com.dominio.portal.IIFServiceservice webService = new br.com.dominio.portal.IIFServiceservice();

        xmlPublicacoes = webService.ObterPublicacoesPorGrupo(empresa, token, 3997, "01/12/2015");

        MessageBox.Show(xmlPublicacoes); //Aqui o texto com erro de encoding

XML Retorno:

XML Retorno

  • 1

    Is the client you are using from the web service WCF? As the class IIFServiceservice was created (i.e., by hand, using "Add Service Reference", etc.)?

  • Using "Add Service Reference". @carlosfiguer

  • Have you ever tried using UTF-8 ? I have no problems using this encoding.

  • @Jcsaint cannot change the return of Webservice. It already returns in ISO-8859-1.

  • take a look at this post http://stackoverflow.com/a/8778384/2588695

2 answers

2

As you are using the "Add Service Reference", what Visual Studio generates for you is a client that uses WCF. WCF, by default, uses XML readers that are optimized for certain encodings (UTF-8, UTF-16LE, UTF-16BE), and does not support others (such as ISO-8859-1).

But WCF is quite extensible, so you can swap the XML reader (XmlReader) which is used by your Find so that it can read (without problems) the other encodings as well. One of samples from WCF shows how you do it - (Custom Text Encoder, or the Portuguese version, that has an automatic translation that is not very good).

Just a warning: prepare to write (or copy) a bit of code. As I said, WCF is very extensible, but it’s not very concise. You’ll have to change the Binding used in your class IIFServiceservice for one who wears a new BindingElement, which creates a new class derived from MessageEncoderFactory, which creates a new class derived from MessageEncoder. The sample has these classes for you to copy.

0

You can add a Web Reference instead of a Service Reference:

Add Service Reference -> Advanced... -> Add Web Reference... -> Type WSDL / URL -> Add Reference

inserir a descrição da imagem aqui

Then set the request encoding:

WS client = new WS();
client.RequestEncoding = Encoding.GetEncoding("ISO-8859-1");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.