做了几个小时,完善了一下我的RSS Reader,附带部分源代码

现在比较像样子了,还有一个收藏夹的功能要做就更像样子了~然后扩展一下Feed类,让它支持更多种类的RSS格式.

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace d_RSS_Reader
{
    public class Feed
    {
        public delegate void ParseCompleteEventHandler();
        public event ParseCompleteEventHandler OnParseComplete;
        public delegate void DownloadEventHandler(float value);
        public event DownloadEventHandler OnDownload;

        private string kind;
        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();
            if(url.Contains("http:"))
                this.url = url;
            else
               
this.url=@"http://"+url;
        }

        public void Parse()
        {
            Thread ParseThread = new Thread(new ThreadStart(this.ParseURL));
            ParseThread.Start();
        }
        public void ParseURL()
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                long responseFileSize = response.ContentLength;
                FileStream tempFile = new FileStream("temp.dat", FileMode.Create, FileAccess.Write);
                byte[] buffer = new byte[100];
                int read=0;
                long allRead = 0;
                while ((read = responseStream.Read(buffer, 0, 100)) != 0)
                {
                    allRead += read;
                    if(OnDownload!=null)
                        OnDownload(100f * allRead / responseFileSize);
                    tempFile.Write(buffer, 0, read);
                }
                tempFile.Dispose();
                responseStream.Dispose();
            }
            catch (Exception e)
            {
                MessageBox.Show("发生错误,错误内容:/n" + e.Message,"d-RSS-Reader");
            }
            ParseFile("temp.dat");
            if (OnParseComplete != null)
            {
                OnParseComplete();
            }
        }
        public void ParseFile(string filePath)
        {
            if (filePath == null)
            {
                throw new Exception("url is not defined");
            }
            FileStream feedfile = new FileStream(filePath, 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);
                    }
                }
            }
            feedfile.Dispose();
        }
        private void ParseChannel(XmlTextReader 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();
                            if (reader.LocalName == "date")
                                pubDate = reader.ReadString();
                        }
                    }
                    catch (XmlException e)
                    {
                    }
                }
                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName=="channel")
                {
                    IsParsed = true;
                    Count = items.Count;
                    return;
                }
            }
        }
        private void ParseItem(XmlTextReader 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 == "date")
                        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 Item this[int index]
        {
            get { return (Item)items[index]; }
        }
        public string Kind
        {
            get{return kind;}
            set { kind = value; }
        }
        public string URL
        {
            get { return url; }
            set
            {
                if (value.Contains("http:"))
                    this.url = value;
                else
                    this.url = @"http://" + 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 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; }
        }

        public override string ToString()
        {
            return title;
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值