1

I am trying to use an encoding page that is not in the common initialized encoding pages in C#.

var greekEncoding = System.Text.Encoding.GetEncoding("1253");
using (var httpClient = new HttpClient())
        {
            var httpContent = new StringContent(xmlString, greekEncoding, "text/xml");
            return await httpClient.PostAsync(baseUrl, httpContent);
        }

System.Text.Encoding has a function GetEncoding("iso-8859-7") or GetEncodings() which returns all the possible encoding pages.

When I try to retrieve a encoding page such as "ISO-8859-7" or "Windows-1253" the Encoding Pages are not registered by default and the result is unrecognizable characters.

How can I register ALL Code Pages in my dotnet core Solution ?

  • 3
    Are you saying that `System.Text.Encoding.GetEncoding("iso-8859-7")` doesn't work without your answer? Can you edit your question or answer to more clearly link these two (and explain why)? – ProgrammingLlama Feb 10 '22 at 08:32
  • Also note that you have tagged your question `[.net]` (i.e. .NET Framework). This is not an issue on .NET Framework, only on .NET Core (`[.net-core]`). I suggest you update your tagging accordingly. – ProgrammingLlama Feb 10 '22 at 08:33
  • @Llama , Hello I tried to find this solution about 2 days. And I share my knowledge in the stackoverflow. Thank you in advance. – Βασίλειος Βαλασίδης Feb 10 '22 at 08:34
  • 3
    To "use all encodings" as in the question, I expected the answer to be a `for` loop which goes through all the encodings and make use of them. What is the problem? – Thomas Weller Feb 10 '22 at 08:34
  • 2
    @ΒασίλειοςΒαλασίδης I understand the problem you encountered, and why the solution you've found is correct, but for a question and answer to be considered "good" on this website, there must be a defined problem, and an answer that explains why it solves that problem. Edit: Your edit is at least a little clearer now. – ProgrammingLlama Feb 10 '22 at 08:35
  • @ThomasWeller https://learn.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=net-6.0#remarks – GSerg Feb 10 '22 at 08:36
  • 1
    @Llama I will explain better my solution .... – Βασίλειος Βαλασίδης Feb 10 '22 at 08:36
  • There are some very close duplicates, e.g. [this](https://stackoverflow.com/questions/37870084/net-core-doesnt-know-about-windows-1252-how-to-fix/37870346) and [this](https://stackoverflow.com/questions/33579661/encoding-getencoding-cant-work-in-uwp-app/33579717), but the first recommends installing a nuget package (which hasn't been necessary for ages) and is tagged vb.net, while the second talks about UWP specifically... – canton7 Feb 10 '22 at 08:39
  • 3
    @GSerg: so the question is "How to use an encoding that is not in the list of the 8 supported code pages by .NET Core?" – Thomas Weller Feb 10 '22 at 08:40
  • 2
    @ThomasWeller Yes. I could not figure that from the question either. – GSerg Feb 10 '22 at 08:41

1 Answers1

6

After a lot of days of searching I found in .net-core, we have to insert a registration in the Encoding.

The Solution in this problem is to Register a provider in the Encoding Class.

EncodingProvider provider = CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(provider);

Just put this two lines in Startup.cs or Program.cs or Main.cs and your solution will have registered provider to use all charsets that is not common.

Common Encoding Pages :

  1. UTF7
  2. UTF8
  3. UTF32
  4. Unicode
  5. UTF-16BE (BigEndianUnicode)
  6. ASCII
  7. Latin1
  8. Default (Computer encoding page)

The solution has been explained in this web page (https://learn.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=net-6.0#remarks) without example.

This misunderstanding appeared in dotnet core.