当我没有Google Reader Atom Schema时,如何使用xml来linq?

时间:2021-03-25 07:18:01

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.

我正在为BlogEngine.Net开发一个小部件。我的小部件将采用一个人的共享项原子提要并打印标题,网站网址,日期和原子网址。为了完成这项任务,我开始使用Linq和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?

这是问题所在。 Google阅读器使用的原子Feed位于source元素中,该元素的属性为gr:stream-id。显然,XName不喜欢名字中有冒号。我不得不走这条路,因为Google Reader架构不存在。如果我有这个架构,它将解决我的问题。我怎样才能获得架构?

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!

如果还有其他方法可以使用Linq和XML,请告诉我。提前致谢!

1 个解决方案

#1


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.

我确实找到了获取原子输入网址的替代解决方案。但是,我仍然更喜欢使用Google Reader Atom Schema,因为这样可以提供一些清晰的代码。

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

在atom feed中,有一个id元素,如下所示:

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

So I added the following to my Linq code:

所以我在Linq代码中添加了以下内容:

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

With getUrl looking like this:

getUrl看起来像这样:

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

该代码返回http://www.domain.com/blog/rss.xml

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

这个解决方案似乎围绕命名空间的使用,但它完成了工作。

#1


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.

我确实找到了获取原子输入网址的替代解决方案。但是,我仍然更喜欢使用Google Reader Atom Schema,因为这样可以提供一些清晰的代码。

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

在atom feed中,有一个id元素,如下所示:

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

So I added the following to my Linq code:

所以我在Linq代码中添加了以下内容:

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

With getUrl looking like this:

getUrl看起来像这样:

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

该代码返回http://www.domain.com/blog/rss.xml

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

这个解决方案似乎围绕命名空间的使用,但它完成了工作。