Simple RSSReader

这里做了一个简单的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;
            }

        }

    }

这个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();
        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值