Reputation: 1095
OK, i'm building a windows phone twitter app, and I can't get this data parsed out for some reason. I'm just trying to parse out a single status...
Here is the xml data from twitter...
<status>
<created_at>Sat Sep 10 17:59:12 +0000 2011</created_at>
<id>112585933307645952</id>
<text>AP: Start 'em/Sit 'em Week 1 - Arrowhead Pride (blog): Midwest Sports FansAP: Start 'em/Sit 'em Week 1Arrowhead ... http://t.co/rWnx5pe</text>
<source><a href="http://twitterfeed.com" rel="nofollow">twitterfeed</a></source>
<truncated>false</truncated>
<favorited>false</favorited>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<in_reply_to_screen_name></in_reply_to_screen_name>
<retweet_count>0</retweet_count>
<retweeted>false</retweeted>
<user>
<id>27680614</id>
<name>Fantasy Football</name>
<screen_name>hackhype</screen_name>
<location>Atlanta, GA</location>
<description>NFL News and Fantasy Perspective!</description>
<profile_image_url>http://a1.twimg.com/profile_images/509176461/icon_normal.gif</profile_image_url>
<profile_image_url_https>https://si0.twimg.com/profile_images/509176461/icon_normal.gif</profile_image_url_https>
<url>http://www.facebook.com/hackhype</url>
<protected>false</protected>
<followers_count>29888</followers_count>
<profile_background_color>ebebeb</profile_background_color>
<profile_text_color>333333</profile_text_color>
<profile_link_color>0084B4</profile_link_color>
<profile_sidebar_fill_color>ebebeb</profile_sidebar_fill_color>
<profile_sidebar_border_color>040470</profile_sidebar_border_color>
<friends_count>6789</friends_count>
<created_at>Mon Mar 30 17:01:37 +0000 2009</created_at>
<favourites_count>1</favourites_count>
<utc_offset>-18000</utc_offset>
<time_zone>Quito</time_zone>
<profile_background_image_url>http://a2.twimg.com/profile_background_images/44228452/twitterbackground.jpg</profile_background_image_url>
<profile_background_image_url_https>https://si0.twimg.com/profile_background_images/44228452/twitterbackground.jpg</profile_background_image_url_https>
<profile_background_tile>false</profile_background_tile>
<profile_use_background_image>true</profile_use_background_image>
<notifications>false</notifications>
<geo_enabled>false</geo_enabled>
<verified>false</verified>
<following>true</following>
<statuses_count>10219</statuses_count>
<lang>en</lang>
<contributors_enabled>false</contributors_enabled>
<follow_request_sent>false</follow_request_sent>
<listed_count>466</listed_count>
<show_all_inline_media>false</show_all_inline_media>
<default_profile>false</default_profile>
<default_profile_image>false</default_profile_image>
<is_translator>false</is_translator>
</user>
<geo />
<coordinates />
<place />
<possibly_sensitive>false</possibly_sensitive>
<contributors />
<entities>
<user_mentions />
<urls>
<url end="135" start="116">
<url>http://t.co/rWnx5pe</url>
<display_url>bit.ly/ookpnp</display_url>
<expanded_url>http://bit.ly/ookpnp</expanded_url>
</url>
</urls>
<hashtags />
</entities>
</status>
Here is the code I'm using to parse it out (it's not working). It's compiling and running, but "thisTweet" is coming back as null....
XElement xmlData = XElement.Parse(e.Result);
thisTweet = (from tweet in xmlData.Descendants("status")
select new Tweet
{
created_at = tweet.Element("created_at").Value,
text = tweet.Element("text").Value,
//user info
name = tweet.Element("user").Element("name").Value,
profile_image_url = tweet.Element("user").Element("profile_image_url").Value,
screen_name = tweet.Element("user").Element("screen_name").Value,
user_id = tweet.Element("user").Element("id").Value
}).First<Tweet>();
DataContext = thisTweet;
Upvotes: 1
Views: 841
Reputation: 570
Twitter also exposes a JSON feed for the same data, this is probably a bit friendlier to your users data/internet mobile subscription as JSON tends to have less overhead than XML. then if you use JSON, you could use datacontracts to deserialize the response which will make your code more manageble.
for a howto check: Use JSON in WP7 instead of SOAP
Upvotes: 0
Reputation: 160962
Your XML does not have descendants named "status" - status is the root element. You are looking to parse a single tweet anyway so why not just:
XElement tweet = XElement.Parse(e.Result);
var thisTweet = new Tweet()
{
created_at = tweet.Element("created_at").Value,
text = tweet.Element("text").Value,
//user info
name = tweet.Element("user").Element("name").Value,
profile_image_url = tweet.Element("user").Element("profile_image_url").Value,
screen_name = tweet.Element("user").Element("screen_name").Value,
user_id = tweet.Element("user").Element("id").Value
};
Upvotes: 3
Reputation: 635
Status is the root of your XML. Your LINQ query would work if multiple status nodes would be present in your xml.
Replace Descendant with DescendantAndSelf and it will work in your case, without modifying anything else.
Upvotes: 1