Select encoding/charset on Windows Phone

Topics: Developer Forum
Sep 1, 2012 at 7:11 PM
Edited Sep 1, 2012 at 7:12 PM

Using HAP 1.4.6 on Windows Phone 7.0. Non-ASCII characters getting garbled. How can I set the encoding in the *Async methods? 

AutoDetectEncoding/OverrideEncoding seem not available on Windows Phone.

The page I am scraping (baseAddress) has "<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">" , but seems like HAP defaults to UTF8.               

 

HtmlWeb.LoadAsync(baseAddress, (s, args) =>
{
    if (args.Document != null)
    {
        this.Items.Add(new ItemViewModel()
        {
            string text = doc.DocumentNode.SelectSingleNode("//td[@align=\"center\"]").InnerText.Trim();
Sep 3, 2012 at 1:57 AM

OK found the answer. Looks like Silverlight only supports utf-8/utf-16 out of the box, so you have to write your own encoding support. (generate with http://www.hardcodet.net/2010/03/silverlight-text-encoding-class-generator)

The corrected code would be something like:

HtmlWeb web = new HtmlWeb();
//CustomEncoding is "iso-8859-2"
CustomEncoding enc = new CustomEncoding();
web.LoadCompleted += new EventHandler<HtmlDocumentLoadCompleted>(_DownLoadCompleted);
web.LoadAsync(baseAddress, enc);

Oct 3, 2012 at 8:00 PM

How about instead of using this ecoding tool do this:

web.LoadAsync(baseUrlAddress, Encoding.GetEncoding("iso-8859-1"));

Works for me in WP 7.1 sdk.

 

Oct 6, 2012 at 6:15 AM

Strange. According to this: http://msdn.microsoft.com/en-us/library/t9a3kf7c(v=vs.95).aspx the only valid values are utf-8 and utf-16 variants. My code threw exception when I tried using iso-8859-2.