views:

248

answers:

2

I'm trying to pull the contents of an RSS feed into an object that can be manipulated in code. It looks like the SyndicationFeed and SyndicationItem classes in .NET 3.5 will do what I need, except for one thing. Every time I've tried to read in the contents of an RSS feed using the SyndicationFeed class, the .Content element for each SyndicationItem is null.

I've run my feed through FeedValidator and have tried this with feeds from several other sources, but to no avail.

XmlReader xr = XmlReader.Create("http://shortordercode.com/feed/");
SyndicationFeed feed = SyndicationFeed.Load(xr);

foreach (SyndicationItem item in feed.Items)
{
    Console.WriteLine(item.Title.Text);
    Console.WriteLine(item.Content.ToString());
}

Console.ReadLine();

I suspect I may just be missing a step somewhere, but I can't seem to find a good tutorial on how to consume RSS feeds using these classes.

EDIT: Thanks to SLaks I've figured out that the issue is with WordPress's use of as the content tag. This doesn't appear to be a problem with the WP Atom feeds so I'll go with that as a solution for now. Thanks SLaks!

+1  A: 

Use the Summary property.

The RSS feed you linked to puts its content in the <description> element.
As documented, the <description> element of an RSS feed maps to the Summary property.

SLaks
The summary property only provides a small amount of the content from the feed, does it not?
kdmurray
It provides the `<description>` element.
SLaks
But I'm looking to get the full text of the articles/blog posts. Am I going about it wrong?
kdmurray
In looking at the feed, it's the `Content` element I'm looking to read.
kdmurray
The feed is non-standard. (`<content:encoded>`)
SLaks
Hmm.. will have to keep looking I guess. That's the way all WordPress feed's do the content tag.
kdmurray
@kdmurray - Did you find a way of getting the full blog posts from any feed irrespective of whether it's encoded or not?
@user102533 - The short answer is no -- at least not with the MS framework. I'm looking into some third party tools to make this work.
kdmurray
A: 

Take a look what I did:

    XmlReader reader = XmlReader.Create("http://kwead.com/blog/?feed=atom");
    SyndicationFeed feed = SyndicationFeed.Load(reader);
    reader.Close();

    foreach (SyndicationItem item in feed.Items)
    {
        string data = item.PublishDate.ToString();
        DateTime dt = Convert.ToDateTime(data);

        string titulo = " - " + item.Title.Text + "<br>";
        string conteudo = ((TextSyndicationContent)item.Content).Text;

        Response.Write(dt.ToString("d"));
        Response.Write(titulo);
        Response.Write(conteudo);
     }
eduardo
This seems to work correctly for atom feeds, unfortunately the RSS spec allows for extensions, which is what the content:encoded thing is. It's valid, and that's what's preventing the item.content piece from working correctly.
kdmurray