2

设想

我正在使用一个看起来像这样的 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,但我不确定语法,也无法在任何地方找到示例。

4

1 回答 1

3

这是一个简短的 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()
于 2010-04-01T17:22:49.580 回答