0

I know it's kinda popular question, but still I didn't find any solution.

So I am getting the xml file as the response from api, but Cyrillic symbols represents as question marks.

enter image description here

I have tried to convert source array of bytes using Encoding class.

using (var httpClient = new HttpClient())
{
   var response = await httpClient.GetAsync(uriBuilder.Uri);
   var responseBytes = await response.Content.ReadAsByteArrayAsync();
   var responseText = Encoding.UTF7.GetString(responseBytes);
   var utf8 = Encoding.UTF8.GetString(responseBytes);
   var unicode = Encoding.Unicode.GetString(responseBytes);
   var ascii = Encoding.ASCII.GetString(responseBytes);
   var defaultt = Encoding.Default.GetString(responseBytes);
}

But still there is no luck.

Charset in response:

Content-Type: text/xml;charset=windows-1251;

Could anyone suggest any solutions please ?

Update: the answer is my comment below

Andrew
  • 591
  • 2
  • 12
  • 33
  • What are you using to view results? I do not think it is the deserialize, it is the viewer. – jdweng Jun 24 '20 at 15:43
  • @jdweng it's Rider from JetBrains – Andrew Jun 24 '20 at 15:50
  • 1
    It looks like a console window. So you have to set the properties of the console to match the encoding. Note sure if you change default encoding if that will help. See : https://www.jetbrains.com/help/idea/configuring-individual-file-encoding.html – jdweng Jun 24 '20 at 15:54

3 Answers3

0

As noted here you can use Encoding.GetEncoding(1251).GetString(responseBytes); to get the desired encoding.

Keep in mind that the operating system needs to support the code page.

ArneJ
  • 1
  • 1
0

So the solution is:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encoding = Encoding.GetEncoding("windows-1251");
var result = encoding.GetString(responseBytes);
Andrew
  • 591
  • 2
  • 12
  • 33
0

windows-10-terminal-encoding

View the current code page:

chcp

Change the code page to Unicode/1251:

chcp 1251

Alex Granovsky
  • 2,996
  • 1
  • 15
  • 10