日期:2014-01-23  浏览次数:20974 次

想把别的网站的RSS新闻整合到自己的网页,刚学,一直都不会。今天终于成功了!可能这个方法有点笨!思路是把XML文件下载到本地,然后再读本地XML,就简单了!不知道是否有更简单的方法,希望高手赐教!

核心代码如下:
(用webRequest得到指定页的内容,然后用FileStream写到本地文件中)
核心部份(下载RSS链接的XML文件到本地!)
 
       private void download_Click(object sender, System.EventArgs e)
                {
                        Encoding sjis = Encoding.GetEncoding("utf-8");
                        FileStream fs = new FileStream(Server.MapPath(".")+"\\sina.xml",FileMode.Create,FileAccess.Write);
                        //建立StreamWriter为写做准备
                        StreamWriter rw = new StreamWriter(fs,sjis);
                       
                        string rl;
                                //创建WebRequest对象
                                WebRequest myReq = WebRequest.Create("http://rss.sina.com.cn/news/marquee/ddt.xml");//读取远程文件(新浪新闻),然后用FileStream将文件保存到本地!
                                //创建WebResponse对象
                                WebResponse myRes = myReq.GetResponse();
                                //获取Stream
                                Stream resStream = myRes.GetResponseStream();
                                //以下操作为读取Stream对象中的内容
                                StreamReader sr = new StreamReader(resStream,sjis);
                                StringBuilder sb = new StringBuilder();
                                while((rl=sr.ReadLine())!=null)
                                {<