Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

c# - Reading non-standard elements in a SyndicationItem with SyndicationFeed

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

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>

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should give you an idea on how to do it:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...