views:

68

answers:

2

Hi,

does anyone know how to load a UTF8-encoded string using WebBrowser.NavigateToString() method? For now I end up with a bunch of mis-displayed characters.

Here's the simple string that won't display correctly:

webBrowser.NavigateToString("ąęłóńżźćś");

The code file is saved with UTF-8 encoding (with signature).

Thanks.

+1  A: 

First up, NavigateToString() is expecting a full html document.

Secondly, as you're passing HTML, it's best to pass HTML entities, rather than relying on encodings. Unfortunately, snt that many entity codes are actually supported by the browser so you should look at using the numeric unicode values where necessary.
Much like this:

webBrowser1.NavigateToString("<html><body><p>&oacute; &#213;</p></body></html>");
Matt Lacey
Thanks, in my app I use full HTML documents. That was just the example. I'm also aware tehat using HTML entities work. But isn't there any way to just pass a UTF-8-encoded doc to NavigateToString()??
Immortal
@Immortal IE7 mobile seems to really struggle with encodings. numeric values is the most reliable solution I've found. :(
Matt Lacey
@Matt Lacey I don't think you're being fair on IE in this particular case. The System.String class can only represent strings using UTF16, so IE assumes that is what the string contains. If you have UTF8 in the form in the question you need to either convert it to a string (i.e. UTF16) first, or use a stream. In the example in the question I don't see a UTF8 string, and neither does IE. What I see is a UTF16 string containing some eastern european letters.
Stewart
A: 

If you have the UTF8 in memory in a byte array then you could try NavigateToStream with a MemoryStream rather than using NavigateToString. You should try to ensure their is a BOM on the UTF8 buffer if you can. Note that the string in the question is not a UTF8 string. It is a UTF16 string with some garbage in it. By placing zeros between the bytes and storing it in a System.String you corrupted it.

Stewart
Unfortunately there's no NavigateToStream() method on the WP7 platform.
Immortal
Ah. That spoils that plan then.
Stewart

related questions