Silverlight and wp7 (windows phone 7) do not support gb2312 decoding by default,
Therefore, most of the Html downloaded from the internet is garbled.
Example: http://news.sina.com.cn/s/2011-11-25/120923524756.shtml
The following shows a wp7 program.
1 WebClient webClenet = new WebClient ();
2 webClenet. DownloadStringAsync (new Uri ("http://news.sina.com.cn/s/2011-11-25/120923524756.shtml", UriKind. RelativeOrAbsolute ));
3 webClenet. DownloadStringCompleted + = new DownloadStringCompletedEventHandler (webClenet_DownloadStringCompleted );
4
5
6
7
8
9
10 callback event:
11 void webClenet_DownloadStringCompleted (object sender, DownloadStringCompletedEventArgs e)
12 {
13 string s = e. Result;
14}
Debugging found
Almost all are garbled issues.
Setting the encoding to UTF-8 is also garbled.
Therefore, an open-source library HtmlAgilityPack (including encoding and processing of HTML nodes) is referenced)
For: http://www.codeplex.com/htmlagilitypack
Reference HtmlAgilityPack. dll to the project. A prompt will pop up, probably because this is not a windows phone class library. Ignore it and confirm it directly.
Then modify our code.
WebClient webClenet=new WebClient();
webClenet.Encoding = new HtmlAgilityPack.Gb2312Encoding(); //加入这句设定编码
webClenet.DownloadStringAsync(new Uri("http://news.sina.com.cn/s/2011-11-25/120923524756.shtml", UriKind.RelativeOrAbsolute));
webClenet.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClenet_DownloadStringCompleted);
Debug the result
Finally we can see our Chinese.
At the same time, HtmlAgilityPack not only helps us solve the gb2312 encoding problem, it is also a powerful tool for us to parse HTML ~!
Sorry, I forgot to make up the case. I made up the case today.
Http://115.com/file/e6abw15h