使用SyndicationFeed读取SyndicationItem中的非标准元素

时间:2008-11-26 02:34:38

标签: c# rss syndication-feed syndication-item

使用.net 3.5,有一个SyndicationFeed将加载到RSS源中,并允许您在其上运行LINQ。

以下是我正在加载的RSS示例:

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"> 
<channel> 
    <title>Title of RSS feed</title> 
    <link>http://www.google.com</link> 
    <description>Details about the feed</description> 
    <pubDate>Mon, 24 Nov 08 21:44:21 -0500</pubDate> 
    <language>en</language> 
    <item> 
        <title>Article 1</title> 
        <description><![CDATA[How to use StackOverflow.com]]></description> 
        <link>http://youtube.com/?v=y6_-cLWwEU0</link> 
        <media:player url="http://youtube.com/?v=y6_-cLWwEU0" /> 
        <media:thumbnail url="http://img.youtube.com/vi/y6_-cLWwEU0/default.jpg" width="120" height="90" /> 
        <media:title>Jared on StackOverflow</media:title> 
        <media:category label="Tags">tag1, tag2</media:category> 
        <media:credit>Jared</media:credit> 
        <enclosure url="http://youtube.com/v/y6_-cLWwEU0.swf" length="233" type="application/x-shockwave-flash"/> 
    </item> 
</channel>

当我循环浏览项目时,我可以通过SyndicationItem的公共属性获取标题和链接。

我似乎无法弄清楚如何获取机箱标签的属性或媒体标签的值。我尝试使用

SyndicationItem.ElementExtensions.ReadElementExtensions<string>("player", "http://search.yahoo.com/mrss/")

对这两种方法有任何帮助吗?

5 个答案:

答案 0 :(得分:38)

这可以让你知道如何做到这一点:

using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Xml.Linq;

SyndicationFeed feed = reader.Read();

foreach (var item in feed.Items)
{
    foreach (SyndicationElementExtension extension in item.ElementExtensions)
    {
        XElement ele = extension.GetObject<XElement>();
        Console.WriteLine(ele.Value);
    }
}

答案 1 :(得分:16)

以下是我使用SyndicationFeed从Feed中检索机箱链接的方法。

static void Main(string[] args)
{
    var feedUrl = "http://blog.stackoverflow.com/index.php?feed=podcast";

    using (var feedReader = XmlReader.Create(feedUrl))
    {
        var feedContent = SyndicationFeed.Load(feedReader);

        if (null == feedContent) return;

        foreach (var item in feedContent.Items)
        {
            Debug.WriteLine("Item Title: " + item.Title.Text);

            Debug.WriteLine("Item Links");
            foreach (var link in item.Links)
            {
                Debug.WriteLine("Link Title: " + link.Title);
                Debug.WriteLine("URI: " + link.Uri);
                Debug.WriteLine("RelationshipType: " + link.RelationshipType);
                Debug.WriteLine("MediaType: " + link.MediaType);
                Debug.WriteLine("Length: " + link.Length);
            }
        }
    }
}

输出如下:

  

项目标题:播客#50
  项目链接
  链接标题:
  URI:http://blog.stackoverflow.com/2009/04/podcast-50/
  RelationshipType:交替
  的MediaType:
  长度:0
  链接标题:
   URI:http://itc.conversationsnetwork.org/audio/download/ITC.SO-Episode50-2009.04.21.mp3
   RelationshipType:enclosure
  MediaType:audio / mpeg
  长度:36580016

您可以从其关系类型中识别机箱链接。

答案 2 :(得分:14)

无论是检索扩展元素还是XElement项的非XML内容,您可能需要考虑使用通用帮助函数,如:

private static T GetExtensionElementValue<T>(SyndicationItem item, string extensionElementName)
{
       return item.ElementExtensions.First(ee => ee.OuterName == extensionElementName).GetObject<T>();
}

根据是否保证元素存在或者是否将其放入可重用的库中,您可能需要添加额外的防御性编程。

答案 3 :(得分:10)

您可以使用LINQ和XPathNavigator的组合来提取订阅源项的联合扩展(基于扩展的名称空间URI)。对于项目附件,您需要检查项目链接集合,以查找RelationshipType为 enclosure 的链接。

示例:

HttpWebRequest webRequest   = WebRequest.Create("http://www.pwop.com/feed.aspx?show=dotnetrocks&filetype=master") as HttpWebRequest;

using (Stream stream = webRequest.GetResponse().GetResponseStream())
{
    XmlReaderSettings settings  = new XmlReaderSettings();
    settings.IgnoreComments     = true;
    settings.IgnoreWhitespace   = true;

    using(XmlReader reader = XmlReader.Create(stream, settings))
    {
        SyndicationFeed feed    = SyndicationFeed.Load(reader);

        foreach(SyndicationItem item in feed.Items)
        {
            // Get values of syndication extension elements for a given namespace
            string extensionNamespaceUri            = "http://www.itunes.com/dtds/podcast-1.0.dtd";
            SyndicationElementExtension extension   = item.ElementExtensions.Where<SyndicationElementExtension>(x => x.OuterNamespace == extensionNamespaceUri).FirstOrDefault();
            XPathNavigator dataNavigator            = new XPathDocument(extension.GetReader()).CreateNavigator();

            XmlNamespaceManager resolver    = new XmlNamespaceManager(dataNavigator.NameTable);
            resolver.AddNamespace("itunes", extensionNamespaceUri);

            XPathNavigator authorNavigator      = dataNavigator.SelectSingleNode("itunes:author", resolver);
            XPathNavigator subtitleNavigator    = dataNavigator.SelectSingleNode("itunes:subtitle", resolver);
            XPathNavigator summaryNavigator     = dataNavigator.SelectSingleNode("itunes:summary", resolver);
            XPathNavigator durationNavigator    = dataNavigator.SelectSingleNode("itunes:duration", resolver);

            string author   = authorNavigator != null ? authorNavigator.Value : String.Empty;
            string subtitle = subtitleNavigator != null ? subtitleNavigator.Value : String.Empty;
            string summary  = summaryNavigator != null ? summaryNavigator.Value : String.Empty;
            string duration = durationNavigator != null ? durationNavigator.Value : String.Empty;

            // Get attributes of <enclosure> element
            foreach (SyndicationLink enclosure in item.Links.Where<SyndicationLink>(x => x.RelationshipType == "enclosure"))
            {
                Uri url             = enclosure.Uri;
                long length         = enclosure.Length;
                string mediaType    = enclosure.MediaType;
            }
        }
    }
}

答案 4 :(得分:7)

您错过了命名空间。使用LINQPad和您的示例Feed:

string xml = @"
    <rss version='2.0' xmlns:media='http://search.yahoo.com/mrss/'> 
    <channel> 
        <title>Title of RSS feed</title> 
        <link>http://www.google.com</link> 
        <description>Details about the feed</description> 
        <pubDate>Mon, 24 Nov 08 21:44:21 -0500</pubDate> 
        <language>en</language> 
        <item> 
            <title>Article 1</title> 
            <description><![CDATA[How to use StackOverflow.com]]></description> 
            <link>http://youtube.com/?v=y6_-cLWwEU0</link> 
            <media:player url='http://youtube.com/?v=y6_-cLWwEU0' /> 
            <media:thumbnail url='http://img.youtube.com/vi/y6_-cLWwEU0/default.jpg' width='120' height='90' /> 
            <media:title>Jared on StackOverflow</media:title> 
            <media:category label='Tags'>tag1, tag2</media:category> 
            <media:credit>Jared</media:credit> 
            <enclosure url='http://youtube.com/v/y6_-cLWwEU0.swf' length='233' type='application/x-shockwave-flash'/> 
        </item> 
    </channel>
    </rss>
    ";



XElement rss = XElement.Parse( xml );
XNamespace media = "http://search.yahoo.com/mrss/";

var player = rss.Element( "channel" ).Element( "item" ).Element(media + "player").Attribute( "url" );
player.Dump();

结果:url =“http://youtube.com/?v=y6_-cLWwEU0”

要查看的构造是:元素(媒体+“播放器”)告诉Linq使用'media'表示的命名空间以及元素名称'player'。

我必须设置脑损伤,我以为你在使用Linq。无论如何,您需要考虑名称空间。