65.9K
CodeProject 正在变化。 阅读更多。
Home

获取网站 RSS Feed 列表的类

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.33/5 (6投票s)

2007年8月14日

CPOL
viewsIcon

67269

downloadIcon

374

一个非常简单的类,用于列出网站的 RSS 订阅源。

Screenshot - Untitled-1__Small_.gif

Screenshot - Untitled-2__Small_.gif

Screenshot - Untitled-3.gif

引言

这是一个非常简单的类,用于获取网站的 RSS 订阅源列表。只需在构造函数中解析网站 URL,即可开始使用……

示例

FeedListOfWebsite MyFeedList = new FeedListOfWebsite(
   new Uri("https://codeproject.org.cn"));
if (MyFeedList.Success)
{
    foreach (FeedDetail fd in MyFeedList.FeedDetails)
    {
        //Some code handle here....
    //fd.Name 
    //fd.Url
    }
}

代码

这是类的构造函数

public FeedListOfWebsite(Uri WebsiteUrl)
//Constructor where you have to parse the Url to look for feed.
{
    Regex RegX = new Regex("<link.*type=\"application " + 
                 "feedsonwebsite="RegX.Matches(GetHtml(WebsiteUrl));" /> 0)
                 //If we find some link tags then...
    {
        //Set FeedDetail Array
        FeedDetails = new FeedDetail[FeedsOnWebsite.Count];
        int fdi = 0;        //      Array index count up value
        foreach (Match Feed in FeedsOnWebsite)
        //Loop through the link tags
        {
            //Extract data from the html line
            FeedDetails[fdi] = ExtractFeed(Feed.Value.ToString());
            fdi++;      //Count Array index 1 up
        }
        _success = true;
    }
}

这是类的私有函数

private string GetHtml(Uri UriPath)
//a Function for getting HTML code of a website.
{
    try
    {
        //Create a Response
        HttpWebResponse Hwr = (HttpWebResponse)WebRequest.Create(UriPath).GetResponse();
        Stream Hwrstrm = Hwr.GetResponseStream();        //Get streamet data
        StreamReader HwrSr = new StreamReader(Hwrstrm);  //Create a streamreader
        string strHTML = HwrSr.ReadToEnd();              //Read all data from website.
        HwrSr.Dispose();                                 //Dispose object
        Hwrstrm.Dispose();                               //Dispose object
        Hwr.Close();                                     //Close object
        return strHTML;                                  //Return HTML code of website
    }
    catch
    {
        return "";  //Return empty string apon error.
    }
}


private FeedDetail ExtractFeed(string HtmlLine)
//a Function for extracting feed data from a HTML code
{
    string name = "";
    string url = "";
    
    #region Find The Title

    try
    {
        Match Title = Regex.Match(HtmlLine, "(?<=title=).*");
        if (Title.Success)
        {
            int EndOfTitle = Title.Value.ToString().IndexOf("\"", 1);
            if (EndOfTitle == -1)
            { EndOfTitle = Title.Value.ToString().IndexOf("'", 1); }
            name = Title.Value.ToString().Substring(0, EndOfTitle);
            name = name.Replace("\"", "").Replace("'", "");
        }
    }
    catch { name = "[Error finding name...]"; }

    #endregion

    #region Find the url

    try
    {
        Match Url = Regex.Match(HtmlLine, "(?<=href=).*");
        if (Url.Success)
        {
            int EndOfHref = Url.Value.ToString().IndexOf(" ", 1);
            if (EndOfHref == -1) { EndOfHref = Url.Value.ToString().IndexOf("\"", 1); }
            if (EndOfHref == -1) { EndOfHref = Url.Value.ToString().IndexOf("'", 1); }
            url = Url.Value.ToString().Substring(0, EndOfHref);
            url = url.Replace("\"", "").Replace("'", "");
        }
    }
    catch { url = ""; }

    #endregion

    return new FeedDetail(new Uri(url), name);
}

历史

  • 在 Marc Jacobi 指出了一些基本问题后,更新了代码。
  • 我添加了一个类,只是为了说明如何使用 RSS 订阅源。
  • 添加的类如下

    • RssFeed
    • RssFeedEntries (RssFeed 的子类,保存订阅源中的所有条目)
    • RssFeedEntry (RssFeedEntries 的子类,保存订阅源中每个条目的数据)

    我没有时间为这些类编写描述。我很抱歉……所以它以原样发布。

© . All rights reserved.