windows phone 7,sliverlight 下载网页的解析,关于wp7 gb2312编码

简介: 原文:windows phone 7,sliverlight 下载网页的解析,关于wp7 gb2312编码关于silverlight和wp7(windows phone 7)是默认不支持gb2312解码的, 所以从网上下载的Html大部分都是乱码。
原文: windows phone 7,sliverlight 下载网页的解析,关于wp7 gb2312编码

关于silverlight和wp7(windows phone 7)是默认不支持gb2312解码的,

所以从网上下载的Html大部分都是乱码。

例如:http://news.sina.com.cn/s/2011-11-25/120923524756.shtml

下面是演示一个wp7程序

 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 回调事件:
11 void webClenet_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
12 {
13 string s= e.Result;
14 }

调试发现

 

几乎全部是乱码问题.

将编码设为utf-8同样是乱码。

于是引用了一个开源的库HtmlAgilityPack(包含编码还处理HTML节点)

下载地址为:http://www.codeplex.com/htmlagilitypack

将HtmlAgilityPack.dll引用到项目中,这时会弹出一个提示,大概就是这不是一个windows phone的类库,不理会,直接确定。

然后修改一下我们的代码

  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);

 

调试一下,结果如图:

 

终于看到我们的中文啦。

同时,HtmlAgilityPack不仅帮我们解决了gb2312的编码问题,它还是我们解析HTML的利器哦~!  

 

 

 

不好意思,之前忘记补上案例了,今天补上

下载地址

http://115.com/file/e6abw15h 

 

 

目录
相关文章
|
5月前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 10 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 10 月更新)
1349 2
Windows Server 2022 中文版、英文版下载 (2025 年 10 月更新)
|
5月前
|
安全 物联网 API
Windows 11 25H2 | 24H2 | 23H2 中文版、英文版 (x64、ARM64) 下载 (2025 年 10 月更新)
Windows 11 25H2 | 24H2 | 23H2 中文版、英文版 (x64、ARM64) 下载 (2025 年 10 月更新)
1635 1
Windows 11 25H2 | 24H2 | 23H2 中文版、英文版 (x64、ARM64) 下载 (2025 年 10 月更新)
|
5月前
|
安全 Unix 物联网
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 10 月更新)
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 10 月更新)
576 0
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 10 月更新)
|
5月前
|
存储 SQL 人工智能
Windows Server 2025 中文版、英文版下载 (2025 年 10 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 10 月更新)
809 0
|
5月前
|
安全 物联网 API
Windows 11 25H2 中文版、英文版 (x64、ARM64) 下载 (2025 年 9 月发布)
Windows 11 25H2 中文版、英文版 (x64、ARM64) 下载 (2025 年 9 月发布)
4401 15
Windows 11 25H2 中文版、英文版 (x64、ARM64) 下载 (2025 年 9 月发布)
|
8月前
|
Linux Windows
Windows 10/11从官网下载ISO的方法
本文介绍了两种从微软官网下载Windows 10/11 ISO镜像的方法。一是通过修改浏览器User Agent为Linux系统,使官网提供ISO下载链接;二是使用UUPDUMP工具,从官网下载并转换为ISO格式,支持最新开发版,操作简便。
|
6月前
|
存储 SQL 人工智能
Windows Server 2025 中文版、英文版下载 (2025 年 9 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 9 月更新)
3248 3
Windows Server 2025 中文版、英文版下载 (2025 年 9 月更新)
|
6月前
|
安全 Unix 物联网
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 9 月更新)
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 9 月更新)
1231 2
|
6月前
|
物联网 虚拟化 Windows
Windows 10 version 22H2 中文版、英文版下载 (2025 年 9 月更新)
Windows 10 version 22H2 中文版、英文版下载 (2025 年 9 月更新)
2221 2
Windows 10 version 22H2 中文版、英文版下载 (2025 年 9 月更新)

推荐镜像

更多
  • DNS