C # - XML return from WebService with coding error (ISO-8859-1)

5

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

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

How do I 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 Return:

    
asked by anonymous 01.12.2015 / 19:45

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 very extensible, so you can change the XML reader ( XmlReader ) that is used by your encoder so that it can read (without problems) the other encodings as well. One of the samples of WCF shows you how to do this - ( Custom Text Encoder , or the Portuguese version , which has a translation automatic that is not very good).

Just a warning: prepare to write (or copy) a bit of code. As I said, WCF is pretty extensible, but it's nothing short of concise. You will have to change the Binding used in your IIFServiceservice class to one that uses 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.

    
01.12.2015 / 21:18
0

You can add Web Reference instead of Service Reference :

  

Add Service Reference - > Advanced ... - > Add Web Reference ... - > WSDL report / URL - > Add Referece

Next,definetherequestencoding:

WSclient=newWS();client.RequestEncoding=Encoding.GetEncoding("ISO-8859-1");
    
05.07.2018 / 14:52