如何使用LINQ to XML阅读计算器RSS订阅 [英] How to read stackoverflow rss feed using linq to xml

查看:103
本文介绍了如何使用LINQ to XML阅读计算器RSS订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读使用LINQ to XML堆栈溢出的RSS提要。我无法获得进入节点,它返回空列表。这是我到目前为止已经试过,任何人可以指出我在做什么错在这里?

在这里,我结合网格视图:

 私人无效StackoverflowFeedList()
{
    grdFeedView.DataSource = StackoverflowUtils.GetStackOverflowFeeds();
    grdFeedView.DataBind();
}

这是这将让所有订阅方法:

 公共静态的IEnumerable< StackOverflowFeedItems> GetStackOverflowFeeds()
{
    的XNamespace,SNMP =htt​​p://www.w3.org/2005/Atom;
    的XDocument的RSSFeed = XDocument.Load(@http://stackoverflow.com/feeds);
    在RssFeed.Descendants从项目VAR帖=(进入)
                选择新StackOverflowFeedItems
                {
                   QuestionID = item.Element(SNMP +ID)。价值,
                   QuestionTitle = item.Element(SNMP +题)。价值,
                   AUTHORNAME = item.Element(SNMP +名)。价值,
                   CategoryTag =(从item.Elements类别(SNMP +类别)
                                  排序依据类别
                                  选择category.Value).ToList()
                   CreatedDate = DateTime.Parse(item.Element(SNMP +刊登)。值)
                   QuestionSummary = item.Element(SNMP +摘要)。价值
                };
    返回posts.ToList();
}

这是我使用的类绑定:

 公共类StackOverflowFeedItems
{
    公共字符串QuestionID {搞定;组; }
    公共字符串QuestionTitle {搞定;组; }
    公共IEnumerable的<串GT; CategoryTag {搞定;组; }
    公共字符串AUTHORNAME {搞定;组; }
    公众的DateTime CreatedDate {搞定;组; }
    公共字符串QuestionSummary {搞定;组; }
}


解决方案

您使用的不是你声明的名称空间的变量。尝试使用

  RssFeed.Descendants(SNMP +项)

(27:11的所有的其他地方,你指的是特定的名字。)

我不是说一定的的所有的你需要什么来解决,但它是最明显的问题。你也应该考虑使用的XElement XAttribute 而不是属性,如:

  CreatedDate =(DateTime的)item.Element(SNMP +发布)

我也建议你多注意压痕和命名局部变量时,使用pascalCase一致。 (相当为什么命名空间变量称为 SNMP 是另一个古怪......剪切和粘贴?)

I am trying read rss feed of stack overflow using Linq to xml. I am unable to get the entry nodes, as it is returning empty list. This I've tried so far, can any one point out what i am doing wrong here?

Here I am binding to the grid view:

private void StackoverflowFeedList()
{
    grdFeedView.DataSource = StackoverflowUtils.GetStackOverflowFeeds();
    grdFeedView.DataBind();
}

This is the method which will get all feeds:

public static IEnumerable<StackOverflowFeedItems> GetStackOverflowFeeds ()
{
    XNamespace Snmp = "http://www.w3.org/2005/Atom";
    XDocument RssFeed = XDocument.Load(@"http://stackoverflow.com/feeds");
    var posts = from item in RssFeed.Descendants("entry")
                select new StackOverflowFeedItems
                {
                   QuestionID = item.Element(Snmp +"id").Value,
                   QuestionTitle = item.Element(Snmp +"title").Value,
                   AuthorName = item.Element(Snmp +"name").Value,
                   CategoryTag = (from category in item.Elements(Snmp +"category")
                                  orderby category
                                  select category.Value).ToList(),
                   CreatedDate = DateTime.Parse(item.Element(Snmp +"published").Value),
                   QuestionSummary = item.Element(Snmp +"summary").Value
                };
    return posts.ToList();
}

And this is the class I am using for binding:

public class StackOverflowFeedItems
{
    public string   QuestionID { get; set; }
    public string   QuestionTitle { get; set; }
    public IEnumerable<string> CategoryTag { get; set; }
    public string AuthorName { get; set;  }
    public DateTime CreatedDate { get; set; }
    public string QuestionSummary { get; set; }
}

解决方案

You're not using the namespace variable you've declared. Try using

RssFeed.Descendants(Snmp + "entry")

(And likewise for all other places where you're referring to particular names.)

I'm not saying that's necessarily all of what you need to fix, but it's the most obvious problem. You should also consider using the explicit conversions of XElement and XAttribute instead of the Value property, e.g.

CreatedDate = (DateTime) item.Element(Snmp +"published")

I'd also encourage you to pay more attention to indentation, and use pascalCase consistently when naming local variables. (Quite why the namespace variable is called Snmp is another oddity... cut and paste?)

这篇关于如何使用LINQ to XML阅读计算器RSS订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆