在rss feed的.aspx文件上使用SyndicationClient [英] Using SyndicationClient on .aspx files for rss feed

查看:92
本文介绍了在rss feed的.aspx文件上使用SyndicationClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用syndicationclient进行rss feed的retreival。



peoblem是在运行应用程序后任务正常工作

没有例外

但是饲料项目是空的。



饲料项目是:QuestionRss和ArticleRss



链接是:

http:// www。 codeproject.com/webservices/QuestionRSS.aspx

http://www.codeproject .com / webservices / ArticleRSS.aspx



我以前用于检索的代码是

i'm using the syndicationclient for the retreival of the rss feeds.

the peoblem is after running the app the tasks are working fine
no exceptions
but the feed items are null.

the feed items are : QuestionRss and ArticleRss

the links are :
http://www.codeproject.com/webservices/QuestionRSS.aspx
http://www.codeproject.com/webservices/ArticleRSS.aspx

the code i used to retrive is

private ObservableCollection<feedData> _feeds = new ObservableCollection<feedData>();
        public ObservableCollection<feedData> feeds
        {
            get { return _feeds; }
        }

        public async Task GetFeedsAsync()
        {
            Task<feedData> newArticles = GetFeedAsync("http://www.codeproject.com/WebServices/ArticleRSS.aspx");

            feeds.Add( await newArticles);

            Task<feedData> newQuestions = GetFeedAsync("http://www.codeproject.com/webservices/QuestionRSS.aspx");

            feeds.Add(await newQuestions);
        }

        private async Task<feedData> GetFeedAsync(string uri)
        {
            SyndicationClient client = new SyndicationClient();
            Uri feedUri = new Uri(uri);
            try
            {
                SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);
                feedData FeedData = new feedData();
                if(feed.Title!= null && feed.Title.Text!= null)
                {
                    FeedData.title = feed.Title.Text;
                }
                if(feed.Subtitle!= null && feed.Subtitle.Text!=null)
                {
                    FeedData.description = feed.Subtitle.Text;
                }
                if (feed.Items!= null && feed.Items.Count>0)
                {
                    //use the lastest date as the last updated date.
                    FeedData.pubTime = feed.Items[0].PublishedDate.DateTime;
                    foreach (SyndicationItem item in feed.Items)
                    {
                        feedItem FeedItem = new feedItem();
                        if(item.Title!= null && item.Title.Text!= null)
                        {
                            FeedItem.title = item.Title.Text;
                        }
                        if(item.PublishedDate!= null)
                        {
                            FeedItem.postTime = item.PublishedDate.DateTime;
                        }
                        if (item.Authors != null && item.Authors.Count > 0)
                        {
                            FeedItem.author = item.Authors[0].Name.ToString();
                        }
                        if (item.Content != null && item.Content.Text != null)
                        {
                            FeedItem.content = item.Content.Text;
                        }

                        if (feed.SourceFormat == SyndicationFormat.Atom10)
                        {
                            if (item.Content!=null && item.Content.Text!= null)
                            {
                                FeedItem.content = item.Content.Text;
                            }
                            if(item.Id!= null)
                            {
                                FeedItem.link = new Uri("http://www.codeproject.com/WebServices/ArticleRSS.aspx");
                            }
                        }
                        else if (feed.SourceFormat==SyndicationFormat.Rss20)
                        {
                            if(item.Summary!= null && item.Summary.Text!= null)
                            { FeedItem.content = item.Summary.Text; }
                            if(item.Links!= null && item.Links.Count>0)
                            {
                                FeedItem.link = item.Links[0].Uri;
                            }
                        }
                    }
                }
                return FeedData;
            }
            catch (Exception ex)
            {

                throw new NotImplementedException(ex.HResult.ToString());
            }
        }





我使用过的数据模型是





the data models i've used are

//holds info for a single blog feed including , including a list of blog posts(fees item).
    public class feedData
    {
        public string  title { get; set; }
        public string description { get; set; }
        public DateTime pubTime { get; set; }
        private List<feedItem> _feedItems = new List<feedItem>();
        public List<feedItem> feedItems { get { return _feedItems; } }

    }

    //holds each blog post's content
    public class feedItem
    {
        public string title { get; set; }
        public string author { get; set; }
        public string content { get; set; }
        public DateTime postTime { get; set; }
        public Uri link{ get; set; }
    }

推荐答案

看起来这是一个Windows应用商店应用程序;我必须进行一些更改才能让您的代码在.NET Framework的桌面版本中运行。



您没有在Feed中获取任何项目的原因是因为您永远不会将 feedItem 对象添加到 feedData <上的 feedItems 集合中/ code> class:

It looks like this is a Windows Store application; I had to make several changes to get your code to work in the desktop version of the .NET Framework.

The reason you're not getting any items in your feed is because you're never adding the feedItem object to the feedItems collection on your feedData class:
foreach (SyndicationItem item in feed.Items)
{
    feedItem FeedItem = new feedItem();
    ...
    
    // Add this line:
    FeedData.feedItems.Add(FeedItem);
}





注意:您的类和变量名称不符合标准命名约定。类,方法和属性应使用句子命名( FeedData FeedItem FeedItems 等);字段和局部变量应使用驼峰大小写( feedItem feedData 等)命名。通过忽略这些约定,您的代码难以阅读。



NB: Your class and variable names don't follow the standard naming conventions. Classes, methods and properties should be named using sentence case (FeedData, FeedItem, FeedItems, etc.); fields and local variables should be named using camel case (feedItem, feedData, etc.). By ignoring these conventions, you're making your code harder to read.


这篇关于在rss feed的.aspx文件上使用SyndicationClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆