简体   繁体   English

SyndicationItem.Content为Null

[英]SyndicationItem.Content is Null

I'm trying to pull the contents of an RSS feed into an object that can be manipulated in code. 我正在尝试将RSS提要的内容提取到可以在代码中操作的对象中。 It looks like the SyndicationFeed and SyndicationItem classes in .NET 3.5 will do what I need, except for one thing. 看起来.NET 3.5中的SyndicationFeed和SyndicationItem类将完成我的需要,除了一件事。 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. 每次我尝试使用SyndicationFeed类读取RSS提要的内容时,每个SyndicationItem的.Content元素都为null。

I've run my feed through FeedValidator and have tried this with feeds from several other sources, but to no avail. 我通过FeedValidator运行我的Feed,并尝试使用来自其他几个来源的Feed,但无济于事。

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. 我怀疑我可能只是在某个地方错过了一个步骤,但我似乎无法找到关于如何使用这些类消费RSS提要的好教程。

EDIT: Thanks to SLaks I've figured out that the issue is with WordPress's use of as the content tag. 编辑:感谢SLaks我已经发现问题是WordPress使用内容标签。 This doesn't appear to be a problem with the WP Atom feeds so I'll go with that as a solution for now. 这似乎不是WP Atom提要的问题所以我现在将其作为解决方案。 Thanks SLaks! 谢谢SLaks!

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

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

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

Use the Summary property. 使用Summary属性。

The RSS feed you linked to puts its content in the <description> element. 您链接的RSS源将其内容放在<description>元素中。
As documented , the <description> element of an RSS feed maps to the Summary property. 如文档所述,RSS提要的<description>元素映射到Summary属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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