I want to integrate RSS news from other websites into my own web page. I have just learned how to do it, but I have never been able to do it. Finally succeeded today! Maybe this method is a bit stupid! The idea is to download the XML file to the local and then read the local XML. That's easy! I don’t know if there is an easier way, I hope experts can enlighten me!
The core code is as follows:
(Use webRequest to get the content of the specified page, and then use FileStream to write it to a local file)
Core part (download the XML file of the RSS link to the local!)
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 );
//Create StreamWriter to prepare for writing
StreamWriter rw = new StreamWriter(fs,sjis);
stringrl;
//Create WebRequest object
WebRequest myReq = WebRequest.Create(" http://rss.sina.com.cn/news/marquee/ddt.xml");//Read the remote file (Sina News), and then use FileStream to save the file locally!
//Create WebResponse object
WebResponse myRes = myReq.GetResponse();
//Get Stream
Stream resStream = myRes.GetResponseStream();
//The following operations are to read the content in the Stream object
StreamReader sr = new StreamReader(resStream,sjis);
StringBuilder sb = new StringBuilder();
while((rl=sr.ReadLine())!=null)
{
rw.WriteLine(rl);
}
myRes.Close();
rw.Flush();
//Close the rw object
rw.Close();
fs.Close();
msg.Text="Download successful!";
}
You can add a judgment in the program, once in a few hours, or once a day!