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 WP7Program
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 ourCode
WebClient webclenet = New WebClient (); |
Webclenet. Encoding = New Htmlagilitypack. gb2312encoding (); // Add this encoding statement |
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 ~!