Search code examples
c#syndicationdublin-core

How to read "dc:creator" element from an RSS feed using System.ServiceModel.Syndication


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.


Solution

  • 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()