使用 C# 在 .net 中读取 rss 提要的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10399400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:41:08  来源:igfitidea点击:

Best Way to read rss feed in .net Using C#

c#rssxmltextreader

提问by Tronics

What is the best way to read RSS feeds?

阅读RSS 提要的最佳方式是什么?

I am using XmlTextReaderto achieve this. Is there any other best way to do it?

我正在使用它XmlTextReader来实现这一目标。有没有其他最好的方法来做到这一点?

XmlTextReader reader = new XmlTextReader(strURL);

DataSet ds = new DataSet();
ds.ReadXml(reader);

After reading the RSS feedusing XmlTextReader, is there any way I can populate data to ListIteminstead of DataSet?

使用阅读RSS 提要后XmlTextReader,有什么方法可以填充数据ListItem而不是DataSet

enter image description here

enter image description here

回答by SLaks

You're looking for the SyndicationFeedclass, which does exactly that.

您正在寻找SyndicationFeedclass,它正是这样做的。

回答by dlopezgonzalez

Add System.ServiceModelin references

添加System.ServiceModel引用

Using SyndicationFeed:

使用SyndicationFeed

string url = "http://fooblog.com/feed";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();
foreach (SyndicationItem item in feed.Items)
{
    String subject = item.Title.Text;    
    String summary = item.Summary.Text;
    ...                
}

回答by Mahdi jokar

Use this :

用这个 :

private string GetAlbumRSS(SyndicationItem album)
    {

        string url = "";
        foreach (SyndicationElementExtension ext in album.ElementExtensions)
            if (ext.OuterName == "itemRSS") url = ext.GetObject<string>();
        return (url);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string albumRSS;
        string url = "http://www.SomeSite.com/rss?";
        XmlReader r = XmlReader.Create(url);
        SyndicationFeed albums = SyndicationFeed.Load(r);
        r.Close();
        foreach (SyndicationItem album in albums.Items)
        {

            cell.InnerHtml = cell.InnerHtml +string.Format("<br \'><a href='{0}'>{1}</a>", album.Links[0].Uri, album.Title.Text);
            albumRSS = GetAlbumRSS(album);

        }



    }

回答by emilast

This is an old post, but to save people some time if you get here now like I did, I suggest you have a look at the CodeHollow.FeedReaderpackage which supports a wider range of RSS versions, is easier to use and seems more robust. https://github.com/codehollow/FeedReader

这是一篇旧帖子,但为了节省人们一些时间,如果你像我一样现在到达这里,我建议你看看CodeHollow.FeedReader包,它支持更广泛的 RSS 版本,更容易使用并且看起来更健壮. https://github.com/codehollow/FeedReader