0

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; }
}
1

1 Answer 1

4

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?)

Sign up to request clarification or add additional context in comments.

3 Comments

i added that Snmp, but it is not returning any thing. empty results, check my edited question
@Ravi: You're still not doing it on the first call: RssFeed.Descendants("entry").
i am getting Object reference not set to an instance of an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.