程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 獲取網站的RSS聚合到自己的網頁

獲取網站的RSS聚合到自己的網頁

編輯:.NET實例教程

想把別的網站的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();
                    &n
bsp;           //以下操作為讀取Stream對象中的內容
                                StreamReader sr = new StreamReader(resStream,sjis);
                                StringBuilder sb = new StringBuilder();
                                while((rl=sr.ReadLine())!=null)
                                {
                                        rw.WriteLine(rl);                                        
                                }                                
                        myRes.Close();        
                        rw.Flush();
                        //關閉rw對象
                        rw.Close();
                        fs.Close();
                        msg.Text="下載成功!";

                        }

可以在程序中加入判斷,幾個小時下一次,或者一天下一次!
bsp;           //以下操作為讀取Stream對象中的內容
                                StreamReader sr = new StreamReader(resStream,sjis);
                                StringBuilder sb = new StringBuilder();
                                while((rl=sr.ReadLine())!=null)
                                {
                                        rw.WriteLine(rl);                                        
                                }                                
                        myRes.Close();        
                        rw.Flush();
                        //關閉rw對象
                        rw.Close();
                        fs.Close();
                        msg.Text="下載成功!";

                        }

可以在程序中加入判斷,幾個小時下一次,或者一天下一次!
bsp;           //以下操作為讀取Stream對象中的內容
                                StreamReader sr = new StreamReader(resStream,sjis);
                                StringBuilder sb = new StringBuilder();
                                while((rl=sr.ReadLine())!=null)
                                {
                                        rw.WriteLine(rl);                                        
                                }                                
                        myRes.Close();        
                        rw.Flush();
                        //關閉rw對象
                        rw.Close();
                        fs.Close();
                        msg.Text="下載成功!";

                        }

可以在程序中加入判斷,幾個小時下一次,或者一天下一次!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved