这里做了一个简单的RSSReader,功能十分简单,小试牛刀而且.
RSS具体规范可查看http://blogs.law.harvard.edu/tech/rss
首先定义一个class,用来存储RSS
public
class
RSS
... {
public RSS()
...{
this.items = new Dictionary<int, Item>();
}
private string title;
public string Title
...{
get ...{ return title; }
set ...{ title = value; }
}
private Dictionary<int,Item> items;
public Dictionary<int,Item> Items
...{
get ...{ return items; }
set ...{ items = value; }
}
public struct Item
...{
private string title;
public string Title
...{
get ...{ return title; }
set ...{ title = value; }
}
private string link;
public string Link
...{
get ...{ return link; }
set ...{ link = value; }
}
private string pubdate;
public string Pubdate
...{
get ...{ return pubdate; }
set ...{ pubdate = value; }
}
public Item(string t, string l, string p)
...{
this.title = t;
this.link = l;
this.pubdate = p;
}
}
}
... {
public RSS()
...{
this.items = new Dictionary<int, Item>();
}
private string title;
public string Title
...{
get ...{ return title; }
set ...{ title = value; }
}
private Dictionary<int,Item> items;
public Dictionary<int,Item> Items
...{
get ...{ return items; }
set ...{ items = value; }
}
public struct Item
...{
private string title;
public string Title
...{
get ...{ return title; }
set ...{ title = value; }
}
private string link;
public string Link
...{
get ...{ return link; }
set ...{ link = value; }
}
private string pubdate;
public string Pubdate
...{
get ...{ return pubdate; }
set ...{ pubdate = value; }
}
public Item(string t, string l, string p)
...{
this.title = t;
this.link = l;
this.pubdate = p;
}
}
}
这个class用了generic的Dictionary来存储RSS里的item,后面插入用的key则是采用Dictionary里的count,内部struct Item不言而喻,外面的title是表示channel的title.
同时在winform里放了个label,textbox,button,treeview四个控件,lable只是放个静态文本,textbox用来输入url,button用来响应event更新treeview,treeview就用来展现rss. UI方面就介绍到些.
下面是程序的主要部分:
public
partial
class
Form1 : Form
... {
public Form1()
...{
InitializeComponent();
}
private void buttonRead_Click(object sender, EventArgs e)
...{
//读取RSS到doc
XmlTextReader reader = new XmlTextReader(this.textBoxUrl.Text);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
//寻找rss结点
XmlNode rss = FindNode(doc, "rss");
//寻找channel结点
XmlNode channel = FindNode(rss, "channel");
RSS rssobj = new RSS();
//把rss内容写入rssobj
SetChannel(rssobj, channel);
//将rssobj内容显示到treeview
UpdateTreeView(rssobj);
}
private XmlNode FindNode(XmlNode node, string name)
...{
foreach (XmlNode n in node.ChildNodes)
...{
if (n.Name.Equals(name) && n.ChildNodes.Count > 0)
return n;
}
return null;
}
private void SetChannel(RSS rss, XmlNode channel)
...{
foreach (XmlNode n in channel.ChildNodes)
...{
if (n.Name.Equals("description"))
...{
rss.Title = n.InnerText;
}
else if (n.Name.Equals("item"))
...{
rss.Items.Add(rss.Items.Count, CreateItem(n));
}
}
}
private RSS.Item CreateItem(XmlNode node)
...{
string title = "";
string link = "";
string pubdate = "";
foreach (XmlNode n in node.ChildNodes)
...{
switch (n.Name)
...{
case "title":
title = n.InnerText;
break;
case "link":
link = n.InnerText;
break;
case "pubDate":
pubdate = n.InnerText;
break;
}
}
return new RSS.Item(title, link, pubdate);
}
private void UpdateTreeView(RSS rss)
...{
this.treeViewRSS.BeginUpdate();
this.treeViewRSS.Nodes.Clear();
this.treeViewRSS.Nodes.Add(rss.Title);
for(int i=0;i<rss.Items.Count;++i)
...{
this.treeViewRSS.Nodes[0].Nodes.Add(rss.Items[i].Title);
this.treeViewRSS.Nodes[0].Nodes[i].Tag = rss.Items[i].Link;
}
this.treeViewRSS.ExpandAll();
this.treeViewRSS.EndUpdate();
}
}
... {
public Form1()
...{
InitializeComponent();
}
private void buttonRead_Click(object sender, EventArgs e)
...{
//读取RSS到doc
XmlTextReader reader = new XmlTextReader(this.textBoxUrl.Text);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
//寻找rss结点
XmlNode rss = FindNode(doc, "rss");
//寻找channel结点
XmlNode channel = FindNode(rss, "channel");
RSS rssobj = new RSS();
//把rss内容写入rssobj
SetChannel(rssobj, channel);
//将rssobj内容显示到treeview
UpdateTreeView(rssobj);
}
private XmlNode FindNode(XmlNode node, string name)
...{
foreach (XmlNode n in node.ChildNodes)
...{
if (n.Name.Equals(name) && n.ChildNodes.Count > 0)
return n;
}
return null;
}
private void SetChannel(RSS rss, XmlNode channel)
...{
foreach (XmlNode n in channel.ChildNodes)
...{
if (n.Name.Equals("description"))
...{
rss.Title = n.InnerText;
}
else if (n.Name.Equals("item"))
...{
rss.Items.Add(rss.Items.Count, CreateItem(n));
}
}
}
private RSS.Item CreateItem(XmlNode node)
...{
string title = "";
string link = "";
string pubdate = "";
foreach (XmlNode n in node.ChildNodes)
...{
switch (n.Name)
...{
case "title":
title = n.InnerText;
break;
case "link":
link = n.InnerText;
break;
case "pubDate":
pubdate = n.InnerText;
break;
}
}
return new RSS.Item(title, link, pubdate);
}
private void UpdateTreeView(RSS rss)
...{
this.treeViewRSS.BeginUpdate();
this.treeViewRSS.Nodes.Clear();
this.treeViewRSS.Nodes.Add(rss.Title);
for(int i=0;i<rss.Items.Count;++i)
...{
this.treeViewRSS.Nodes[0].Nodes.Add(rss.Items[i].Title);
this.treeViewRSS.Nodes[0].Nodes[i].Tag = rss.Items[i].Link;
}
this.treeViewRSS.ExpandAll();
this.treeViewRSS.EndUpdate();
}
}