阅读非标准元素与SyndicationFeed一个SyndicationItem [英] Reading non-standard elements in a SyndicationItem with SyndicationFeed

查看:188
本文介绍了阅读非标准元素与SyndicationFeed一个SyndicationItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With .net 3.5, there is a SyndicationFeed that will load in a RSS feed and allow you to run LINQ on it.

下面是我加载RSS的例子:

Here is an example of the RSS that I am loading:

<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的公共属性。

When I loop through the items, I can get back the title and the link through the public properties of SyndicationItem.

我似乎无法弄清楚如何让机箱标签的媒体标记的属性,或值。我试图用

I can't seem to figure out how to get the attributes of the enclosure tag, or the values of the media tags. I tried using

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

与其中任一任何帮助吗?

Any help with either of these?

推荐答案

您缺少的命名空间。使用 LINQPad 和您的示例提要:

Your missing the namespace. Using LINQPad and your example 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 =htt​​p://youtube.com/?v=y6_-cLWwEU0

result: url="http://youtube.com/?v=y6_-cLWwEU0"

的结构看为:元(媒体+玩家),它告诉LINQ到使用由psented命名空间重新$ P $'媒体'和元素名球员

The construct to look at is: Element(media + "player") that tells Linq to use the namespace represented by 'media' as well as the element name 'player'.

脑损坏,必须在我的部分设置在,我还以为你是使用LINQ。无论如何,你需要采取的空间考虑。

这篇关于阅读非标准元素与SyndicationFeed一个SyndicationItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆