Monday, January 11, 2010

Add GeoRSS Tag in Atom Feed with C#

You will need to have installed the REST starter kit found here: REST Starter Kit.


Start a new project  and select the Atom Feed WCF Service.  The key to getting a geoRSS tag is using the element and attribute extension methods. First, we need to provide an attribute extension.
XmlQualifiedName geoRssXmlQualifiedName = new XmlQualifiedName("georss", tp://www.w3.org/2000/xmlns/");
string geoRssNamespace = "http://www.georss.org/georss";
SyndicationFeed feed = new SyndicationFeed();
feed.AttributeExtensions.Add(geoRssXmlQualifiedName, geoRssNamespace); //add an attribute extension

Once we have provided the correct attribute extension, add a geoRSS point by using the element extension method.
[syndItem.ElementExtensions.Add(new SyndicationElementExtension("point", geoRssNamespace, "45.256" + " " + "-71.92")); //add georss point       

Now, build and run your project. Right-click the browser window and view source. You should see that your point has been add to your feed and is wrapped with the geoRSS tag, as shown here.


Full Code Sample:
[ServiceBehavior(IncludeExceptionDetailInFaults = true), AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed), ServiceContract]
public partial class FeedService
{
[WebHelp(Comment = "Sample description for GetFeed.")]
[WebGet(UriTemplate = "")]
[OperationContract]
public Atom10FeedFormatter GetFeed()
{
XmlQualifiedName geoRssXmlQualifiedName = new XmlQualifiedName("georss", "http://www.w3.org/2000/xmlns/");
string geoRssNamespace = "http://www.georss.org/georss";
SyndicationFeed feed = new SyndicationFeed();
feed.AttributeExtensions.Add(geoRssXmlQualifiedName, geoRssNamespace); //add an attribute extension

feed.Id = "http://tempuri.org/myFeed";
feed.Title = new TextSyndicationContent("GeoRSS Ready Feed");
feed.Description = new TextSyndicationContent("Sample Feed with GeoRSS Tag");

// Create the list of syndication items. These correspond to Atom entries
List items = new List();

//create syndication items to add to the list
SyndicationItem syndItem = new SyndicationItem();
syndItem.Title = new TextSyndicationContent("I am here");
syndItem.Content = new TextSyndicationContent("This is my first geoRSS point");
syndItem.ElementExtensions.Add(new SyndicationElementExtension("point", geoRssNamespace, "45.256" + " " + "-71.92")); //add georss point
items.Add(syndItem); //add item to list

feed.Items = items; //add list to the feed

feed.AddSelfLink(WebOperationContext.Current.IncomingRequest.GetRequestUri());
// Sets response content-type for Atom feeds
WebOperationContext.Current.OutgoingResponse.ContentType = ContentTypes.Atom;

return feed.GetAtom10Formatter();
}

}

A complete working project sample can be found here: Download Sample

3 comments:

  1. Thanks a lot for this post. It inspired me to implement GeoRSS Tags in my feeds.

    ReplyDelete
  2. It's really nice & useful. But how to add KML link into the SyndItem. So from browser if i click the KML link, it will open up in google earth..

    ReplyDelete
  3. Don't you need to add the "where" element and include the point within that?

    ReplyDelete