SyndicationItem.Content is Null

巧了我就是萌 提交于 2019-12-05 13:38:27

问题


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:


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/")



回答2:


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;
    }



回答3:


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);
     }



回答4:


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.



来源:https://stackoverflow.com/questions/2444540/syndicationitem-content-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!