简易的用于Rss Reader的Rss feed分析类

主要是用到了XmlTextReader,现在只是一个最基本的结构……很多东西以后添加完善。最后自己做一个RSS Reader~

使用方法:

Feed feed1=new Feed("rss.xml");

feed1.Parse();

for(int i=0;i<feed1.Count;i++)

   Console.WriteLine(feed1[i].Title);

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Xml;

namespace d_RSS_Reader
{
    public class Feed
    {
        private string url;
        private string title;
        private string link;
        private string pubDate;
        private ArrayList items;
        public bool IsParsed = false;
        public bool IsLocalFeed = false;
        public int Count;

        public Feed()
        {
            items = new ArrayList();
        }
        public Feed(string url)
        {
            items=new ArrayList();
            this.url = url;
        }
        public void Parse()
        {
            if (url == null)
            {
                throw new Exception("url is not defined");
            }
            FileStream feedfile = new FileStream(url, FileMode.Open,FileAccess.Read);
            XmlTextReader reader = new XmlTextReader(feedfile);
            reader.WhitespaceHandling = WhitespaceHandling.None;
            reader.XmlResolver = null;
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName == "channel")
                    {
                        ParseChannel(reader);
                    }
                }
            }
        }
        private void ParseChannel(XmlReader reader)
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    try
                    {
                        if (reader.LocalName == "item")
                            ParseItem(reader);
                        else if (reader.Depth == 2)
                        {
                            if (reader.LocalName == "title")
                                title = reader.ReadString();
                            if (reader.LocalName == "link")
                                link = reader.ReadString();
                            if (reader.LocalName == "pubDate")
                                pubDate = reader.ReadString();
                        }
                    }
                    catch (XmlException e)
                    {
                    }
                }
                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName=="channel")
                {
                    IsParsed = true;
                    Count = items.Count;
                    return;
                }
            }
        }
        private void ParseItem(XmlReader reader)
        {
            int startDepth = reader.Depth;
            Item newItem = new Item();
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName == "title")
                        newItem.Title = reader.ReadString();
                    if (reader.LocalName == "creator")
                        newItem.Creator = reader.ReadString();
                    if (reader.LocalName == "author")
                        newItem.Creator = reader.ReadString();
                    if (reader.LocalName == "pubDate")
                        newItem.PubDate = reader.ReadString();
                    if (reader.LocalName == "link")
                        newItem.Link = reader.ReadString();
                    if (reader.LocalName == "description")
                        newItem.Description= reader.ReadString();
                }
                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "item")
                    break;
            }
            this.items.Add(newItem);
        }
        public void ParseURL(string url)
        {
        }
        public Item this[int index]
        {
            get { return (Item)items[index]; }
        }
        public string URL
        {
            get { return url; }
            set { url = value; }
        }
        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        public string Link
        {
            get { return link; }
            set { link = value; }
        }
        public string PubDate
        {
            get { return pubDate; }
            set { pubDate = value; }
        }
    }
    public class Item
    {
        private string title;
        private string creator;
        private string pubDate;
        private string link;
        private string description;

        public Item() { }
        public Item(string title, string author, string pubDate, string link, string description)
        {
            this.title = title;
            this.creator = author;
            this.pubDate = pubDate;
            this.link = link;
            this.description = description;
        }
        public override string ToString()
        {
            return title;
        }
        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        public string Creator
        {
            get { return creator; }
            set { creator = value; }
        }
        public string PubDate
        {
            get { return pubDate; }
            set { pubDate = value; }
        }
        public string Link
        {
            get { return link; }
            set { link = value; }
        }
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值