C# Windows Phone 7 XML填充到列表框查询
我有一个XML,它有多个记录。我想使用下面的代码将这些记录填充到列表框中C# Windows Phone 7 XML填充到列表框查询,c#,.net,visual-studio-2010,windows-phone-7,C#,.net,Visual Studio 2010,Windows Phone 7,我有一个XML,它有多个记录。我想使用下面的代码将这些记录填充到列表框中 void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { System.Diagnostics.Debug.WriteLine("Error: "+e);
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
System.Diagnostics.Debug.WriteLine("Error: "+e);
}
XElement coupon = XElement.Parse(e.Result);
MainListBox.ItemsSource = from query in coupon.Descendants("cs")
select new ViewModels.LoadCoupon()
{
CouponName = (string)query.Element("c").Attribute("t"),
//MerchantImage = dB.getBaseUri() + "images/merchant/" + (string)query.Element("ms").Element("m").Element("id")
MerchantImage = dB.getBaseUri() + "images/merchant/" + (string)query.Element("c").Attribute("mId") + ".png"
};
}
其中MainListBox是我的列表框。使用上述代码,我只能填充一条记录。我知道我错过了什么。任何人都可以告诉我,为了显示XML中的多个记录,我需要做什么。我复制了一个我正在使用的示例XML。多谢各位
<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>
<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>
<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>
您只有一个
cs
元素,因此它只生成一个元素。我想你想要这个:
// Note the use of Descendants("c") here
MainListBox.ItemsSource = from query in coupon.Descendants("c")
select new ViewModels.LoadCoupon()
{
CouponName = (string)query.Attribute("t"),
MerchantImage = dB.getBaseUri() +
"images/merchant/" +
(string)query.Attribute("mId") +
".png"
};
编辑:要查找特定元素,我将使用:
var match = coupon.Descendants("c")
.Where(c => (string) c.Attribute("mId") == mId)
.Single();
您只有一个
cs
元素,因此它只生成一个元素。我想你想要这个:
// Note the use of Descendants("c") here
MainListBox.ItemsSource = from query in coupon.Descendants("c")
select new ViewModels.LoadCoupon()
{
CouponName = (string)query.Attribute("t"),
MerchantImage = dB.getBaseUri() +
"images/merchant/" +
(string)query.Attribute("mId") +
".png"
};
编辑:要查找特定元素,我将使用:
var match = coupon.Descendants("c")
.Where(c => (string) c.Attribute("mId") == mId)
.Single();
太棒了,谢谢你。您还可以告诉我如何使用属性值获取元素吗。例如,在我获取mId的Linkq查询中,我希望使用循环中已有的mId获取元素及其名称。谢谢,太棒了,谢谢。您还可以告诉我如何使用属性值获取元素吗。例如,在我获取mId的Linkq查询中,我希望使用循环中已有的mId获取元素及其名称。非常感谢。