unable to parse the xml query using Linq
I am developing a sample Twitter app for Windows phone 7. In my code to display some details of user, used the followi开发者_开发百科ng code.
void ShowProfile()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Profile_DownloadCompleted);
client.DownloadStringAsync(new Uri("http://api.twitter.com/1/users/show.xml?user_id=" + this.id));
}
void Profile_DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{ return; }
if (e.Result == null) MessageBox.Show("NUlllllllllll");
XElement Profile = XElement.Parse(e.Result);
var ProfileDetails = (from profile in Profile.Descendants("user")
select new UserProfile
{
UserName = profile.Element("screen_name").Value,
ImageSource = profile.Element("profile_image_url").Value,
Location = profile.Element("location").Value,
TweetsCount = profile.Element("statuses_count").Value,
}).FirstOrDefault();
LayoutRoot.DataContext = ProfileDetails;
}
Here, LayoutRoot is the Grid name. But the data binding doesn't work. Infact, when kept a Break point it seems there is no data in the ProfileDetails object. But I could observe that e.Result contains the required data in the XML format. Can any body figureout where I am going wrong?? Thanks in advance.
You have used XElement.Parse
so Profile
represents the single root <user>
that API request would have returned. You are then trying to look for user
elements inside it which of course makes no sense.
Try XDocument.Parse
instead. Also does it really make any sense assigning a IEnumerable<UserProfile>
to the data context when that list can only ever contain 1 entry?
精彩评论