当我没有 Google Reader Atom 架构时,如何使用 xml to linq?

发布于 2024-07-12 02:57:29 字数 2106 浏览 2 评论 0 原文

我正在为 BlogEngine.Net 开发一个小部件。 我的小部件将获取一个人的共享项目原子提要并打印标题、网站网址、日期和原子网址。 为了完成这个任务,我已经开始使用Linq和XML。

问题就在这里。 Google Reader 使用的原子提要位于源元素中,该元素具有 gr:stream-id 属性。 显然,XName 不喜欢名称中包含冒号。 我必须走这条路,因为 Google 阅读器架构不存在。 如果我有这个模式,它就会解决我的问题。 我怎样才能获得架构?

下面是到目前为止我的代码的一小段:

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 />";
    }
}

如果在仍然使用 Linq 和 XML 的同时还有另一种方法来解决这个问题,请告诉我。 提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2024-07-19 02:57:29

我确实找到了获取 Atom feed url 的替代解决方案。 不过,我仍然更喜欢使用 Google Reader Atom Schema,因为这样可以提供有点清晰的代码。

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

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

因此,我将以下内容添加到我的 Linq 代码中:

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

getUrl 如下所示:

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文