如何阅读“dc:creator”使用 System.ServiceModel.Syndicate 来自 RSS 提要的元素

发布于 2024-08-27 06:03:35 字数 1150 浏览 8 评论 0原文

场景

我正在使用的 RSS 提要部分看起来像这样:

<item>
    <title>Blog Title Here</title>
    <link>http://blogurl.com/2010/03/23/title/</link>
    <comments>http://blogurl.com/2010/03/23/title/#comments</comments>
    <pubDate>Tue, 23 Mar 2010 10:44:54 +0000</pubDate>
    <dc:creator>AuthorName</dc:creator>
    <category><![CDATA[CategoryName]]></category>
    <guid isPermaLink="false">http://blogurl.com/?p=102</guid>
    <description><![CDATA[Description of post content]]></description>
    <content:encoded><![CDATA[Full blog post here]]></content:encoded>
</item>

我正在使用 Rss20FeedFormatter 来获取 Syndicates 列表,如下所示:

List<SyndicationItem> items;
using (var reader = XmlReader.Create("http://blogurl.com/feed/"))
{
  var formatter = new Rss20FeedFormatter();
  formatter.ReadFrom(reader);
  items = formatter.Feed.Items.ToList();
}

问题

我不知道如何访问值 dc:creator元素。我相信我需要以某种方式使用 ElementExtensions,但我不确定语法,并且在任何地方都找不到示例。

Scenario

I am consuming an RSS feed that looks, in part, like this:

<item>
    <title>Blog Title Here</title>
    <link>http://blogurl.com/2010/03/23/title/</link>
    <comments>http://blogurl.com/2010/03/23/title/#comments</comments>
    <pubDate>Tue, 23 Mar 2010 10:44:54 +0000</pubDate>
    <dc:creator>AuthorName</dc:creator>
    <category><![CDATA[CategoryName]]></category>
    <guid isPermaLink="false">http://blogurl.com/?p=102</guid>
    <description><![CDATA[Description of post content]]></description>
    <content:encoded><![CDATA[Full blog post here]]></content:encoded>
</item>

I am using Rss20FeedFormatter to get a list of SyndicationItems, like this:

List<SyndicationItem> items;
using (var reader = XmlReader.Create("http://blogurl.com/feed/"))
{
  var formatter = new Rss20FeedFormatter();
  formatter.ReadFrom(reader);
  items = formatter.Feed.Items.ToList();
}

Problem

I have no idea how to access the value dc:creator element. I believe that I need to use ElementExtensions somehow, but I'm not sure of the syntax, and can't find examples anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2024-09-03 06:03:35

下面是一个简短的 F# 示例,希望能够引导您使用一些有用的 API:

let xml = @"
<rss version=""2.0"">
<channel xmlns:dc=""http://whatever.it.is/"" xmlns:content=""http://that.too/"">
<title>testing</title>
<item> 
    <title>Blog Title Here</title> 
    <link>http://blogurl.com/2010/03/23/title/</link> 
    <comments>http://blogurl.com/2010/03/23/title/#comments</comments> 
    <pubDate>Tue, 23 Mar 2010 10:44:54 +0000</pubDate> 
    <dc:creator>AuthorName</dc:creator> 
    <category><![CDATA[CategoryName]]></category> 
    <guid isPermaLink=""false"">http://blogurl.com/?p=102</guid> 
    <description><![CDATA[Description of post content]]></description> 
    <content:encoded><![CDATA[Full blog post here]]></content:encoded> 
</item> 
</channel>
</rss>
"

open System.Xml 
open System.IO
open System.ServiceModel.Syndication
let ReadIt() =
    use reader = XmlReader.Create(new StringReader(xml))
    let formatter = new Rss20FeedFormatter()
    formatter.ReadFrom(reader)
    let items = formatter.Feed.Items
    for item in items do
        // if I know there are dc:creator elements (that can deserialize with DataContract of type 'string')
        let dcCreators = item.ElementExtensions.ReadElementExtensions<string>("creator","http://whatever.it.is/")
        for dcc in dcCreators do
            printfn "dcc : %s" dcc
        // if just probing around
        for ee in item.ElementExtensions do
            printfn "extension `%s:%s'" ee.OuterNamespace ee.OuterName 
            use eer = ee.GetReader()
            let inner = eer.ReadInnerXml()
            printfn "    %s" inner
ReadIt()

Here's a short F# sample, that hopefully steers you to some useful APIs:

let xml = @"
<rss version=""2.0"">
<channel xmlns:dc=""http://whatever.it.is/"" xmlns:content=""http://that.too/"">
<title>testing</title>
<item> 
    <title>Blog Title Here</title> 
    <link>http://blogurl.com/2010/03/23/title/</link> 
    <comments>http://blogurl.com/2010/03/23/title/#comments</comments> 
    <pubDate>Tue, 23 Mar 2010 10:44:54 +0000</pubDate> 
    <dc:creator>AuthorName</dc:creator> 
    <category><![CDATA[CategoryName]]></category> 
    <guid isPermaLink=""false"">http://blogurl.com/?p=102</guid> 
    <description><![CDATA[Description of post content]]></description> 
    <content:encoded><![CDATA[Full blog post here]]></content:encoded> 
</item> 
</channel>
</rss>
"

open System.Xml 
open System.IO
open System.ServiceModel.Syndication
let ReadIt() =
    use reader = XmlReader.Create(new StringReader(xml))
    let formatter = new Rss20FeedFormatter()
    formatter.ReadFrom(reader)
    let items = formatter.Feed.Items
    for item in items do
        // if I know there are dc:creator elements (that can deserialize with DataContract of type 'string')
        let dcCreators = item.ElementExtensions.ReadElementExtensions<string>("creator","http://whatever.it.is/")
        for dcc in dcCreators do
            printfn "dcc : %s" dcc
        // if just probing around
        for ee in item.ElementExtensions do
            printfn "extension `%s:%s'" ee.OuterNamespace ee.OuterName 
            use eer = ee.GetReader()
            let inner = eer.ReadInnerXml()
            printfn "    %s" inner
ReadIt()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文