Windows Phone 7 XML populate to List Box error
I am try to do an application which loads a remote XML and populates it to the List box. I followed this tutorial and made half way through. When I have the twitter feed url I am able to populate the content to the phone. But when I try my XML it does not show up on the screen
During this line "System.Diagnostics.Debug.WriteLine(coupon);" I am getting the XML that I am expecting. So I am sure that up to the above code everything works fine.
My Code
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
System.Diagnostics.Debug.WriteLine("Error: "+e);
}
DeviceBroker dB = new DeviceBroker();
XElement coupon = XElement.Parse(e.Result);
System.Diagnostics.Debug.WriteLine(coupon);
MainListBox.ItemsSource = from query in coupon.Descendants("cs")
select new ViewModels.LoadCoupon()
{
CouponName = (string)query.Element("c").Element("t").Value,
//MerchantImage = dB.getBaseUri() + "images/merchant/" + (string)query.Element("ms").Element("m").Element("id")
MerchantImage = dB.getBaseUri() + "images/merchant/" + (string)query.Element("c").Element("mId") + ".png"
};
}
<d>
<ms>
<m id="9921" n="The Book Company" />
<m id="6333" n="Earth Rental" />
<m id="6329" n="The Organic Baker" />
<m id="6331" n="News Stand" />
<m id="6327" n="The Jam Company" />
<m id="6325" n="The Fruit Company" />
</ms>
<cs>
<c id="14533" mId="9921" t="50% Off Any Book Purchase">
<ls>
<l id="40145" lng="-0.0724" lat="51.5024" d="4.97" dim="45.91" intX="" intY="" intL="" />
</ls>
<cats>
<cat id="41" />
<cat id="43" />
</cats>
<as />
</c>
</cs>
</d>
As you can see from my code I am trying to get the d->cs->c->t and d->cs->c->mId elements. I am getting NullReferenceException was unhandeled error. If I take .Value out of the code, I am not getting any error at the same time I am not getting anything on the screen as well. Can anyone please shred some light on it? Please let me know if you need any more info.
My XAML Looks something like this.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="MainListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
开发者_开发百科 <Image Source="{Binding MerchantImage}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<TextBlock Text="{Binding CouponName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
"t" is an attribute of "c", not an element: you should use .Attribute("t") instead of .Element("t")
http://msdn.microsoft.com/en-us/library/bb387086.aspx
精彩评论