For windows phone 7 and sliverlight, download the webpage and parse the 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 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

 

 

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.