For Windows Phone 7 and sliverlight, download the webpage for parsing, about WP7 gb2312 encoding)

Source: Internet
Author: User

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 ~!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.