当前位置:编程学习 > wap >>

wIndows phone 7 解析Html数据

 

在我的上一篇文章中我介绍了windows phone 7的gb2312解码,

http://www.zzzyk.com/kf/201111/112551.html

解决了下载的Html乱码问题,这一篇,我将介绍关于windows phone 7解析html数据,以便我们获得想要的数据.

 

这里,我先介绍一个类库HtmlAgilityPack,(上一篇文章也是通过这个工具来解码的). 类库的dll文件我会随demo一起提供

 

这里,我以新浪新闻为例来解析数据

 

 

 

先看看网页版的新浪新闻

 

http://news.sina.com.cn/w/sd/2011-11-27/070023531646.shtml

 

然后我们看一下他的源文件,

 

发现新闻内容的结构是

 

view sourceprint?<div class="blkContainerSblk"> 

 

                <h1 id="artibodyTitle" pid="1" tid="1" did="23531646" fid="1666">title</h1> 

 

                <div class="artInfo"><span id="art_source"><a href="http://www.sina.com.cn">http://www.sina.com.cn</a></span>  <span id="pub_date">pub_date</span>  <span id="media_name"><a href="">media_name</a> <a href=""></a> </span></div> 

 

  

 

                <!-- 正文内容begin --> 

 

                <!-- google_ad_section_start --> 

 

  

 

                <div class="blkContainerSblkCon" id="artibody"></div> 

 

</div>

 

大部分还有ID属性,这更适合我们去解析了。

 

接下来我们开始去解析

 

第一: 引用HtmlAgilityPack.dll文件

 

第二:用WebClient或者WebRequest类来下载HTML页面然后处理成字符串。

 

view sourceprint?public  delegate void CallbackEvent(object sender, DownloadEventArgs e); 

 

       public  event CallbackEvent DownloadCallbackEvent; 

 

       public void HttpWebRequestDownloadGet(string url) 

 

       { 

 

             

 

           Thread _thread = new Thread(delegate() 

 

           { 

 

               Uri _uri = new Uri(url, UriKind.RelativeOrAbsolute); 

 

               HttpWebRequest _httpWebRequest = (HttpWebRequest)WebRequest.Create(_uri); 

 

                _httpWebRequest.Method="Get"; 

 

               

 

               _httpWebRequest.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult result) 

 

               { 

 

                   HttpWebRequest _httpWebRequestCallback = (HttpWebRequest)result.AsyncState; 

 

                   HttpWebResponse _httpWebResponseCallback = (HttpWebResponse)_httpWebRequestCallback.EndGetResponse(result); 

 

                   Stream _streamCallback = _httpWebResponseCallback.GetResponseStream(); 

 

 

 

                   StreamReader _streamReader = new StreamReader(_streamCallback,new HtmlAgilityPack.Gb2312Encoding()); 

 

                   string _stringCallback = _streamReader.ReadToEnd(); 

 

                  

 

                   Deployment.Current.Dispatcher.BeginInvoke(new Action(() => 

 

                   { 

 

                       if (DownloadCallbackEvent != null) 

 

                       { 

 

                           DownloadEventArgs _downloadEventArgs = new DownloadEventArgs(); 

 

                           _downloadEventArgs._DownloadStream = _streamCallback; 

 

                           _downloadEventArgs._DownloadString = _stringCallback; 

 

                           DownloadCallbackEvent(this, _downloadEventArgs); 

 

 

 

                       } 

 

                   })); 

 

 

 

               }), _httpWebRequest); 

 

           }) ; 

 

           _thread.Start(); 

 

       } 

 

      // }

 

补充:移动开发 , Windows Phone ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,