简体   繁体   中英

How can I serialize a collection of SyndicationItem objects?

I have a collection of SyndicationItem objects that I need to serialize but I'm not sure how I go about doing that. Suggestions?

IEnumberable<SyndicationItem> blogPosts = GetBlogPosts();

...

private IEnumerable<SyndicationItem> GetBlogPosts()
{
  var query = Enumerable.Empty<SyndicationItem>();
  foreach (string feed in Feeds)
  {
    try
    {
      using (XmlReader reader = XmlReader.Create(feed))
      {
        query = query.Union((from item in SyndicationFeed.Load(reader).Items
            orderby item.PublishDate descending
                select item).Take(NumberOfItemsToDisplay));
      }
    }
    catch (Exception ex)
    {
      Console.WriteLine("Invalid Feed: {0} ({1})", feed, ex.Message);
    }
  }
  query = query.OrderByDescending(i => i.PublishDate).Take(NumberOfItemsToDisplay);
  return query;
}

If you want to serialize a SyndicationItem, it has couple of methods you could use SaveAsRss20 , SaveAsAtom10 . To serialize collection, iterate through one by one and write it to some stream

SyndicationItem item = new SyndicationItem("Item Title", "Item Content", new Uri("http://Item/Alternate/Link"), "itemID", DateTimeOffset.Now);
XmlWriter writer = XmlWriter.Create("outfile.xml");
item.SaveAsRss20(writer);
writer.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM