C# 使用 SyndicationFeed 读取 SyndicationItem 中的非标准元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/319591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading non-standard elements in a SyndicationItem with SyndicationFeed
提问by Jared
With .net 3.5, there is a SyndicationFeed that will load in a RSS feed and allow you to run LINQ on it.
在 .net 3.5 中,有一个 SyndicationFeed 将加载到 RSS 提要中,并允许您在其上运行 LINQ。
Here is an example of the RSS that I am loading:
这是我正在加载的 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>
When I loop through the items, I can get back the title and the link through the public properties of SyndicationItem.
当我遍历项目时,我可以通过 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?
有任何帮助吗?
采纳答案by jr.
Your missing the namespace. Using LINQPadand your example feed:
您缺少命名空间。使用LINQPad和您的示例提要:
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();
result: url="http://youtube.com/?v=y6_-cLWwEU0"
结果: url="http://youtube.com/?v=y6_-cLWwEU0"
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'.
要查看的构造是: Element(media + "player"),它告诉 Linq 使用由“media”表示的命名空间以及元素名称“player”。
Brain damage must be setting in on my part, I thought you were using Linq. Anyway, you need to take the namespace into consideration.
脑损伤一定是我造成的,我以为你在使用 Linq。无论如何,您需要考虑命名空间。
回答by Oppositional
You can use a combination of LINQ and XPathNavigator to extract the syndication extensions of a feed item (based on namespace URI of the extension). For item enclosures, you will want to examine the item links collection for links that have a RelationshipType of enclosure.
您可以结合使用 LINQ 和 XPathNavigator 来提取提要项的联合扩展(基于扩展的命名空间 URI)。对于项目附件,您将需要检查项目链接集合中是否有关联类型为附件的链接。
Example:
例子:
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;
}
}
}
}
回答by hitec
Here is how I managed to retrieve the enclosure link from a feed using SyndicationFeed.
以下是我如何使用 SyndicationFeed 从提要中检索附件链接。
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);
}
}
}
}
The output is as follows:
输出如下:
Item Title: Podcast #50
Item Links
Link Title:
URI: http://blog.stackoverflow.com/2009/04/podcast-50/
RelationshipType: alternate
MediaType:
Length: 0
Link Title:
URI: http://itc.conversationsnetwork.org/audio/download/ITC.SO-Episode50-2009.04.21.mp3
RelationshipType: enclosure
MediaType: audio/mpeg
Length: 36580016
项目标题:Podcast #50
项目链接
链接标题:
URI:http: //blog.stackoverflow.com/2009/04/podcast-50/
RelationshipType:alternative
MediaType:
长度:0
链接标题:
URI:http://itc。对话网络.org /audio/ download / ITC.SO-Episode50-2009.04.21.mp3
关系类型:外壳媒体类型
:音频/mpeg
长度:36580016
You can identify the enclosure link from its relationship type.
您可以从其关系类型识别机柜链接。
回答by Ron
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);
}
}
回答by jkade
Whether you're retrieving the non-XML contents of extension elements or XElement items, you might want to consider using a generic helper function like:
无论您是检索扩展元素的非 XML 内容还是 XElement 项目,您都可能需要考虑使用一个通用的辅助函数,例如:
private static T GetExtensionElementValue<T>(SyndicationItem item, string extensionElementName)
{
return item.ElementExtensions.First(ee => ee.OuterName == extensionElementName).GetObject<T>();
}
Depending on whether the elements are guaranteed to be there or whether you are putting this into a reusable library, you may need to add additional defensive programming.
根据元素是否保证存在或您是否将其放入可重用库中,您可能需要添加额外的防御性编程。