Windows Phone 8 SDK WebClient Encoding Issue

c# html-agility-pack visual-studio-2010 windows-phone-7 windows-phone-8

Question

I'm trying to parse html from a site using windows-1254 charset. but all Turkish characters shown like this: � � � � �

Where is the actual problem? I did try these:

webClient.Encoding = System.Text.Encoding.UTF8
webClient.Encoding = System.Text.Encoding.GetString("UTF-8");

as function this:

public string ReplaceText(string _text)
        {
            _text = _text.Replace("Ä°", "Ä°").Replace("ı", "ı").Replace("ü", "ü").Replace("ÅŸ", "ÅŸ").Replace("Ã…", "Åž").Replace("ç", "ç").Replace("ö", "ö").Replace("ÄŸ", "ÄŸ").Replace("Ç", "Ç").Replace("Ö", "Ö").Replace("Ãœ", "Ãœ");
            return _text;
        }

also this headers:

webClient.Headers["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
webClient.Headers["Accept-Charset"] = "windows-1254,utf-8;q=0.7,*;q=0.7";

(with iso-8859-9, utf8 too)

and this is how i am using the webclient:

WebClient wb = new WebClient();         
            wb.Headers["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
            wb.Headers["Accept-Charset"] = "windows-1254,utf-8;q=0.7,*;q=0.7";
            wb.DownloadStringAsync(new Uri("http://www.site.com"));
            wb.Encoding = System.Text.Encoding.UTF8;
            wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DSC);

handler:

HtmlDocument htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(e.Result);

            var inputs = htmlDoc.DocumentNode.SelectNodes("//div[@id=\"mrln-eyhaber\"]//a");

            foreach (var input in inputs)
            {

                textarea.Text += this.ReplaceText(input.Attributes["title"].Value.ToString()) + "\n\n";
            }

Accepted Answer

Instead of using standard approach, why don you create a custom class, specific for your needs, which will handle the etconding.

This will help you generate the class, like so:enter image description here

and then all you have to do

webClient.Encoding = CustomEncoding();

Let me know how it goes (:


Popular Answer

Why did you set the Encoding to UTF-8 if you know its windows-1254? The fix is rather easy, you just have to set the correct encoding in the WebClient.

wb.Encoding = Encoding.GetEncoding(1254);

or

wb.Encoding = Encoding.GetEncoding("windows-1254");

Also, your ReplaceText method shouldn't be needed anymore either.

EDIT: Of course, Windows Phone doesn't support that encoding just like that, you have to implement any encoding other than utf-8 or utf-16 yourself. Luckily there's an easy way to do that, you just have to use the programm described here to generate your own encoding class.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why