我有一个文本框来从用户那里获取用户名,我正在尝试从我的应用程序中检索他的最新推文并且它无法正常工作。当我尝试使用列表框获取用户的所有推文时,其工作正常。 我试过这段代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
WebClient twitter = new WebClient();
twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + username.Text));
}
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//throw new NotImplementedException();
if (e.Error != null)
return;
XElement xmlTweets = XElement.Parse(e.Result);
//trying something
var message = from tweet in xmlTweets.Descendants("status")
select tweet.Element("text").Value;
textBlock1.Text = message.ToString();
}
输出:System.Linq.Enumerable +
答案 0 :(得分:0)
试试这个:
var message = (from tweet in xmlTweets.Descendants("status")
select tweet.Element("text").Value).FirstOrDefault();
if(!String.IsNullOrEmpty(message))
textBlock1.Text = message.ToString();