Sunday, February 22, 2009

Integrating blogger.com with a personal website

My website, andreasbrekken.com, runs ASP.NET MVC and is highly integrated with popular services such as Flickr, Delicious, and Blogger.

So how do you display posts?

In your controller:
this.ViewData["Blog"] = from x in XDocument.Load("http://andreas-brekken.blogspot.com/feeds/posts/default")
.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom")).Take(10)
select x;

In your view:

    <ul class="blog-entries">  
<li>
<h3><%= x.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value%></h3>
<%= x.Element(XName.Get("content", "http://www.w3.org/2005/Atom")).Value %>
<a class="more-link" href="<%= x.Elements(XName.Get("link", "http://www.w3.org/2005/Atom"))
.First(y => y.Attribute("rel").Value == "alternate").Attribute("href").Value %>">Read the entry</a>
</li>
<% } %>
</ul>

It'd be a better idea to process all of the XML in the controller and then pass the posts as strongly typed, or anonymous class, objects.

0 comments:

Post a Comment