I have an application that I’m writing that allows users to search Twitter, written mainly just for fun and learning about how XML and LINQ play together. I’ve written code to fetch the atom feed (example query: http://search.twitter.com/search.atom?q=twitter), and I can verify that it is, in fact, receiving XML.
Just to get started with parsing the document, I figured it would be simple enough to just parse out the content of each tweet. Once I verify that works, then I would move on to the author, then date, and so on and so forth until everything is parsed.
Here is what I’m using to get the content:
var list = from tweet in doc.Element("feed").Descendants("entry")
select new Tweet("AUTHOR", tweet.Element("content").Value, new DateTime(), "TITLE");
As you can see, the document structure looks something like this:
<feed><entry><content></content></entry>.....</feed>
At least as far as we are concerned. I get a NullReferenceException on this line of code, but the debugger shows that the document is not null (it does in fact have the whole feed loaded in it). The previous line calls XDocument.Parse(), which throws no exceptions.
Does anyone know what could be causing my downfall?
The
feedelement includes this:which changes the default namespace for the elements. You should use that like this:
(Just to explain, you were looking for a namespace-less “feed” element; that didn’t exist, so
Elementreturned null. If you’d fixed just that,Descendantswould have returned an empty sequence. If you’d fixed those two,tweet.Element("content")would have returned null, causing a differentNullPointerException.)