views:

311

answers:

1

I am working on a small widget for BlogEngine.Net. My widget is going to take a person's shared items atom feed and print the title, website url, date, and atom url. To complete this task, I have begun to use Linq and XML.

Here is the problem. The atom feed Google Reader uses is located in the source element, which has an attribute of gr:stream-id. Apparently, the XName does not like having colons in the name. I had to go that route because the Google Reader schema does not exist. If I had that schema, it would solve my issue. How can I get the schema?

Below is a small snippet of my code so far:

protected void Page_Load(object sender, EventArgs e) {
    //Replace userid with the unique session id in your Google Reader 
    //url (the numbers after the F)
    getFeed("userid");
}

public class Post {
    public String title { get; set; }
    public DateTime published { get; set; }
    public String postUrl { get; set; }
    public String baseUrl { get; set; }
    public String atomUrl { get; set; }
}

private void getFeed(String userID) {
    String uri = "http://www.google.com/reader/public/atom/user%2F" + userID + "%2Fstate%2Fcom.google%2Fbroadcast";

    XNamespace atomNS = "http://www.w3.org/2005/Atom";
    //The Google Reader schema link does not exist :(
    XNamespace grNS = "http://www.google.com/schemas/reader/atom/";
    XDocument feed = XDocument.Load(uri);

    var posts = from item in feed.Descendants(atomNS + "entry")
                select new Post {
                    title = item.Element(atomNS + "title").Value,
                    published = DateTime.Parse(item.Element(atomNS + "published").Value),
                    postUrl = item.Element(atomNS + "link").Attribute("href").Value,
                    atomUrl = item.Element(atomNS + "source").Attribute(grNS + "href").Value
                };
    foreach (Post post in posts) {
        output.InnerHtml += "Title: " + post.title + "<br />";
        output.InnerHtml += "Published: " + post.published.ToString() + "<br />";
        output.InnerHtml += "Post URL: " + post.postUrl + "<br />";
        output.InnerHtml += "Atom URL: " + post.atomUrl + "<br />";
        output.InnerHtml += "<br />";
    }
}

If there is another way to go about this while still using Linq and XML, please let me know. Thanks in advance!

+1  A: 

I did find an alternative solution to getting the atom feed url. However, I would still prefer to use the Google Reader Atom Schema because that would provide a bit clear code.

In the atom feed, there is an id element, which looks like this:

tag:google.com,2005:reader/feed/http://www.domain.com/blog/rss.xml

So I added the following to my Linq code:

atomUrl = getUrl(item.Element(atomNS + "source").Element(atomNS + "id").Value)

With getUrl looking like this:

private String getUrl(String item) {
    if (!item.Equals("")) {
        return item.Substring(item.IndexOf("http://"));
    }
    return "";
}

That code return http://www.domain.com/blog/rss.xml

This solution seems to run around the use of namespaces, but it gets the job done.

daub815