Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyndicationItem.Content is Null

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!

like image 723
kdmurray Avatar asked Mar 15 '10 01:03

kdmurray


3 Answers

This should get content for you:

SyndicationFeed feed = SyndicationFeed.Load(reader);

string content = feed.ElementExtensions.ReadElementExtensions<string>("encoded", "http://purl.org/rss/1.0/modules/content/")
like image 53
Francis Shanahan Avatar answered Nov 14 '22 11:11

Francis Shanahan


Its due to the fact that it is content:encoded instead of content. To read the content in this case, I am going to use this:

    string content="";
    foreach (SyndicationElementExtension ext in item.ElementExtensions)
    {
        if (ext.GetObject<XElement>().Name.LocalName == "encoded")
            content = ext.GetObject<XElement>().Value;
    }
like image 6
Maxali Avatar answered Nov 14 '22 11:11

Maxali


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);
     }
like image 5
eduardo Avatar answered Nov 14 '22 09:11

eduardo