System.ServiceModel.Syndication.SyndicationFeed;
System.ServiceModel.Syndication.SyndicationItem;
Code ví dụ
[sourcecode language="csharp"]
List<SyndicationItem> rss = new List<SyndicationItem>();
string url = "http://vnexpress.net/rss/the-thao.rss";
XmlReader xmlreader = XmlReader.Create(url);
SyndicationFeed rssfeed = SyndicationFeed.Load(xmlreader);
xmlreader.Close();
foreach (SyndicationItem item in rssfeed.Items)
{
rss.Add(item);
}
[/sourcecode]
Demo ví dụ:
Mã nguồn demo
[sourcecode language="csharp"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<SyndicationItem> rss;
public Form1()
{
InitializeComponent();
this.listBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDoubleClick);
this.button1.Click += new System.EventHandler(this.button1_Click);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void load()
{
rss = new List<SyndicationItem>();
string url = textBox1.Text;
XmlReader xmlreader = XmlReader.Create(url);
SyndicationFeed rssfeed = SyndicationFeed.Load(xmlreader);
xmlreader.Close();
foreach (SyndicationItem item in rssfeed.Items)
{
rss.Add(item);
listBox1.Items.Add(item.Title.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
load();
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (listBox1.SelectedItem == null)
return;
if (e.Clicks == 2)
{
Process.Start(rss[listBox1.SelectedIndex].Links[0].Uri.ToString());
}
}
}
}
[/sourcecode]
Cảm ơn thầy!
ReplyDelete