gpt4 book ai didi

c# - 如何使用 System.ServiceModel.Syndication 从 RSS 提要中读取 "dc:creator"元素

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:03 24 4
gpt4 key购买 nike

场景

我使用的 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 获取 SyndicationItems 列表,如下所示:

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,但我不确定语法,也无法在任何地方找到示例。

最佳答案

这是一个简短的 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()

关于c# - 如何使用 System.ServiceModel.Syndication 从 RSS 提要中读取 "dc:creator"元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2562120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com