Create a GeoRSS Feed in .NET

Last week, I was working on a small team project that leveraged ESRI’s ArcGIS Viewer for Silverlight. We wanted to plot points on the map using latitude and longitude coordinates, something that the viewer supports natively through its GeoRSS widget; all we needed to do is provide a GeoRSS feed!

The RSS feed’s just an XML document, and GeoRSS is just a specific format for an RSS feed. So, it should be no problem to do. I hadn’t created an RSS feed before, so I started by Googling. I figured I’d end up building an XML document using Linq to XML and writing the contents to a page. It was even easier than that, though. The .NET Framework has RSS classes built right in!

Here’s a very simple example of how to create a GeoRSS feed in .NET by using the Rss20FeedFormatter class:

public Rss20FeedFormatter GetItems()
{
    var feed = new SyndicationFeed(
        “My GeoRSS Feed”,
        “A silly little feed”,
        new Uri(“https://adamprescott.net/georss”));

    var items = GetItemsFromDataSource();
    feed.Items = items.Select(
        x =>
        {
            var f = new SyndicationItem(x.Title, x.Description, x.Link, x.Id, DateTime.Now);
            f.PublishDate = x.PubDate;
            f.Summary = new TextSyndicationContent(x.Description);

            XNamespace geons = “http://www.w3.org/2003/01/geo/wgs84_pos#”;
            var lat = new XElement(geons + “lat”, x.Latitude);
            f.ElementExtensions.Add(new SyndicationElementExtension(lat));
            var lon = new XElement(geons + “long”, x.Longitude);
            f.ElementExtensions.Add(new SyndicationElementExtension(lon));
            return f;
        });

    return new Rss20FeedFormatter(feed);
}

Taking it one step further, we needed to host our feed and make it accessible via a web service call.

So, we created an interface…

[ServiceContract]
public interface IRssFeed
{
    [OperationContract]
    [WebGet]
    Rss20FeedFormatter GetItems();
}

And created a new WCF endpoint in our web.config (did I mention this was an ASP.NET application?)…

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name=“webHttpBehavior”>
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <services>
    <service name=“adamprescott.net.GeoRSS.MyItemsFeed”>
      <endpoint binding=“webHttpBinding”
				behaviorConfiguration=“webHttpBehavior”
				contract=“adamprescott.net.GeoRSS.IRssFeed” />
    </service>
  </services>
</system.serviceModel>

And voila–we were done! Browsing to “http://ourserver/ourapplication/MyItemsFeed/GetItems&#8221; gave us our valid GeoRSS feed, and we fed that to our ArcGIS Viewer to plot the points. It all worked swimmingly!

Advertisements

2 thoughts on “Create a GeoRSS Feed in .NET”

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s