Windows phone 7 无法使用Linq分析xml查询
我正在为WindowsPhone7开发一个示例Twitter应用程序。在我的代码中显示用户的一些详细信息,使用了以下代码Windows phone 7 无法使用Linq分析xml查询,windows-phone-7,linq-to-xml,twitter,Windows Phone 7,Linq To Xml,Twitter,我正在为WindowsPhone7开发一个示例Twitter应用程序。在我的代码中显示用户的一些详细信息,使用了以下代码 void ShowProfile() { WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Profile_DownloadCompleted);
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;
}
这里,LayoutRoot是网格名称。但是数据绑定不起作用。
事实上,当保留断点时,ProfileDetails对象中似乎没有数据。但我可以观察到e.Result包含XML格式的必需数据。
有人能知道我哪里出了问题吗??
提前感谢。您已经使用了
XElement.Parse
,因此Profile
表示API请求将返回的单个根
。然后,您试图在其中查找user
元素,这当然毫无意义
尝试
XDocument.Parse
。当列表只能包含一个条目时,将IEnumerable
分配给数据上下文是否真的有意义?谢谢您的回答。我尝试了XDocument.Parse,它成功了。谈到您提出的第二点,事实上Linq查询不返回IEnumerable,而是返回一个UserProfile对象,ProfileDetails保存了该对象。这是因为FirstOrDefault()方法。当然,web查询的响应也会返回单个用户的详细信息,而不是列表。但你消除了我的疑虑,节省了我的时间。再次感谢。