WP7 SyndicationFeed

时间:2012-12-13 16:12:20

标签: c# visual-studio-2010 windows-phone-7 expression-blend

我正在创建一个小型RSS阅读器应用程序。

我用来获取xml的代码如下:

    void PhonePage1_Loaded(object sender, RoutedEventArgs e)
    {
        WebClient wc = new WebClient();
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
        wc.OpenReadAsync(new Uri("http://www.benchmark.pl/rss/aktualnosci-pliki.xml"));
    }

    void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        SyndicationFeed feed;
        using (XmlReader reader = XmlReader.Create(e.Result))
        {
            feed = SyndicationFeed.Load(reader);
            NewsFeed.ItemsSource = feed.Items;
        }
    }

问题是SyndicationFeed项目模板不适合xml文件中的模板。我不知何故需要编辑模板,以便正确插入文本。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

使用SyndicationFeed,你必须使用它定义的对象。但是,很容易将其转换为更接近您的XML的东西。

假设你有一个类:

class MyFeedItem
{
    public string Title { get; set; }
    public Uri Link { get; set; }
    public string Description { get; set; }
    public DateTimeOffset PubDate { get; set; }
}

您可以将SyndicationFeed项目转换为:

private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    SyndicationFeed feed;
    using (XmlReader reader = XmlReader.Create(e.Result))
    {
        feed = SyndicationFeed.Load(reader);
        NewsFeed.ItemsSource = ParseFeed(feed);
    }
}

private static IEnumerable<MyFeedItem> ParseFeed(SyndicationFeed feed)
{
    return feed.Items.Select(item => new MyFeedItem
                                            {
                                                Title = item.Title.Text,
                                                Link = item.Links.First().Uri,
                                                Description = item.Summary.Text,
                                                PubDate = item.PublishDate
                                            });
}