簡體   English   中英

SyndicationItem.Content為Null

[英]SyndicationItem.Content is Null

我正在嘗試將RSS提要的內容提取到可以在代碼中操作的對象中。 看起來.NET 3.5中的SyndicationFeed和SyndicationItem類將完成我的需要,除了一件事。 每次我嘗試使用SyndicationFeed類讀取RSS提要的內容時,每個SyndicationItem的.Content元素都為null。

我通過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();

我懷疑我可能只是在某個地方錯過了一個步驟,但我似乎無法找到關於如何使用這些類消費RSS提要的好教程。

編輯:感謝SLaks我已經發現問題是WordPress使用內容標簽。 這似乎不是WP Atom提要的問題所以我現在將其作為解決方案。 謝謝SLaks!

這應該為您獲取內容:

SyndicationFeed feed = SyndicationFeed.Load(reader);

string content = feed.ElementExtensions.ReadElementExtensions<string>("encoded", "http://purl.org/rss/1.0/modules/content/")

這是因為它是內容:編碼而不是內容。 要閱讀本案例中的內容,我將使用此:

    string content="";
    foreach (SyndicationElementExtension ext in item.ElementExtensions)
    {
        if (ext.GetObject<XElement>().Name.LocalName == "encoded")
            content = ext.GetObject<XElement>().Value;
    }

看看我做了什么:

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

使用Summary屬性。

您鏈接的RSS源將其內容放在<description>元素中。
如文檔所述,RSS提要的<description>元素映射到Summary屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM