user3781243
user3781243

Reputation: 43

How can I encode string in file content?

This is what I did before in form1 constructor:

client.Encoding = System.Text.Encoding.GetEncoding(1255);
page = client.DownloadString("http://rotter.net/scoopscache.html");

client = WebClient variable page = string variable

But I changed it and I'm doing in form1 constructor:

page = OffLineDownload.offlineHtmlFile1();

Now page have the same content as before but without downloading it. How can I get encoding to 1255 now since some of the content in page is in Hebrew?

I tried now this:

page = Encoding.GetEncoding(1255).ToString();
page = OffLineDownload.offlineHtmlFile1();

But it's not working i got error later since the content is not in hebrew yet but some symbols and chars more like gibrish.

The offline class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;

namespace ScrollLabelTest
{
    class OffLineDownload
    {
        static string offLineHtmlBeforeChanged;
        static string OffLineHtmlAfterChanged;

        public static string offlineHtmlFile1()
        {
            offLineHtmlBeforeChanged = File.ReadAllText(@"C:\Temp\news\news1.htm");
            return offLineHtmlBeforeChanged;
        }

        public static string offlineHtmlFile2()
        {
            OffLineHtmlAfterChanged = File.ReadAllText(@"C:\Temp\news\news2.htm");
            return OffLineHtmlAfterChanged;
        }
    }
}

Upvotes: 1

Views: 96

Answers (1)

James
James

Reputation: 9975

From System.Net.WebClient.DownloadString

byte[] bytes = this.DownloadDataInternal(address, out request);
string @string = this.GuessDownloadEncoding(request).GetString(bytes);

which is equivalent to

byte[] bytes = File.ReadAllBytes(filename);
string @string = Encoding.GetEncoding(1255).GetString(bytes);

Although as Hans points out, it's better to do this in one go

string @string = File.ReadAllText(filename, Encoding.GetEncoding(1255));

Upvotes: 1

Related Questions