10

什么是结合feedfeed2的简单方法?我希望将feed2中的项目添加到feed。我还想避免重复,因为当问题同时使用 WPF 和 Silverlight 标记时,提要可能已经有项目。

Uri feedUri = new Uri("http://stackoverflow.com/feeds/tag/silverlight");
XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri);
SyndicationFeed feed = SyndicationFeed.Load(reader);    

Uri feed2Uri = new Uri("http://stackoverflow.com/feeds/tag/wpf");
XmlReader reader2 = XmlReader.Create(feed2Uri.AbsoluteUri);
SyndicationFeed feed2 = SyndicationFeed.Load(reader2);
4

6 回答 6

11

您可以使用 LINQ 简化代码以加入两个列表(不要忘记将 System.Linq 放入您的 usings 中,如果需要,请在项目中参考 System.Core)这是一个 Main 执行联合并将它们打印到控制台(使用正确清理阅读器)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.ServiceModel.Syndication;

namespace FeedUnion
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri feedUri = new Uri("http://stackoverflow.com/feeds/tag/silverlight"); 
            SyndicationFeed feed;
            SyndicationFeed feed2;
            using(XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri))
            {
                feed= SyndicationFeed.Load(reader); 
            }
            Uri feed2Uri = new Uri("http://stackoverflow.com/feeds/tag/wpf"); 
            using (XmlReader reader2 = XmlReader.Create(feed2Uri.AbsoluteUri))
            {
            feed2 = SyndicationFeed.Load(reader2);
            }
            SyndicationFeed feed3 = new SyndicationFeed(feed.Items.Union(feed2.Items));
            StringBuilder builder = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(builder))
            {
                feed3.SaveAsRss20(writer);
                System.Console.Write(builder.ToString());
                System.Console.Read();
            }
        }
    }
}
于 2008-09-17T18:49:42.810 回答
1

好吧,一种可能性是创建一个新的联合提要,它是第一个提要的克隆,然后简单地遍历第二个提要上的每个帖子,检查第一个是否存在,如果不存在则添加它。

类似于以下内容:

SyndicationFeed newFeed = feed.clone;
foreach(SyndicationItem item in feed2.items)
{
  if (!newFeed.contains(item))
    newFeed.items.Add(item);
}

也许能够做到。看起来“项目”是一个简单的可编号的联合项目列表,所以没有理由不能简单地添加它们。

于 2008-09-17T02:41:20.873 回答
0

如果它仅用于 stackoverflow,您可以使用:
https
://stackoverflow.com/feeds/tag/silverlight%20wpf 这合并两个标签。

对于更通用的解决方案,我不知道。您可能必须手动迭代两个提要的元素并将它们连接在一起。您可以比较 <entry> 的 <id> 元素以查看它们是否重复。

于 2008-09-17T02:40:43.387 回答
0

我已经将今天接受的答案变成了一个单元测试,只是为了稍微探索一下:

    [TestMethod]
    public void ShouldCombineRssFeeds()
    {
        //reference: http://stackoverflow.com/questions/79197/combining-two-syndicationfeeds

        SyndicationFeed feed;
        SyndicationFeed feed2;

        var feedUri = new Uri("http://stackoverflow.com/feeds/tag/silverlight");
        using(var reader = XmlReader.Create(feedUri.AbsoluteUri))
        {
            feed = SyndicationFeed.Load(reader);
        }

        Assert.IsTrue(feed.Items.Count() > 0, "The expected feed items are not here.");

        var feed2Uri = new Uri("http://stackoverflow.com/feeds/tag/wpf");
        using(var reader2 = XmlReader.Create(feed2Uri.AbsoluteUri))
        {
            feed2 = SyndicationFeed.Load(reader2);
        }

        Assert.IsTrue(feed2.Items.Count() > 0, "The expected feed items are not here.");

        var feedsCombined = new SyndicationFeed(feed.Items.Union(feed2.Items));

        Assert.IsTrue(
            feedsCombined.Items.Count() == feed.Items.Count() + feed2.Items.Count(),
            "The expected number of combined feed items are not here.");

        var builder = new StringBuilder();
        using(var writer = XmlWriter.Create(builder))
        {
            feedsCombined.SaveAsRss20(writer);
            writer.Flush();
            writer.Close();
        }

        var xmlString = builder.ToString();

        Assert.IsTrue(new Func<bool>(
            () =>
            {
                var test = false;

                var xDoc = XDocument.Parse(xmlString);
                var count = xDoc.Root.Element("channel").Elements("item").Count();
                test = (count == feedsCombined.Items.Count());

                return test;
            }
        ).Invoke(), "The expected number of RSS items are not here.");
    }
于 2012-01-18T19:38:59.933 回答
0
        //Executed and Tested :)            
        using (XmlReader reader = XmlReader.Create(strFeed))
        {
            rssData = SyndicationFeed.Load(reader);
            model.BlogFeed = rssData; ;
        }
        using (XmlReader reader = XmlReader.Create(strFeed1))
        {
            rssData1 = SyndicationFeed.Load(reader);
            model.BlogFeed = rssData1;
        }

        SyndicationFeed feed3 = new SyndicationFeed(rssData.Items.Union(rssData1.Items));
        model.BlogFeed = feed3;           
        return View(model);
于 2013-07-29T06:39:09.323 回答
0

这对我来说很好:

// create temporary List of SyndicationItem's
List<SyndicationItem> tempItems = new List<SyndicationItem>();

// add all feed items to the list
tempItems.AddRange(feed.Items);
tempItems.AddRange(feed2.Items);

// remove duplicates with Linq 'Distinct()'-method depending on yourattributes

// add list without duplicates to 'feed2'
feed2.Items = tempItems
于 2016-02-06T09:55:27.660 回答