Karthik
Karthik

Reputation: 1091

Windows Phone 7 XML populate to List Box query

I have a XML which has multiple records. I would like to populate those records to the listbox using the below code.

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"
                                            };

        }

Where MainListBox is my list box. Using the above code I am able to populate only one record. I know I am missing something. Can anyone please let me know what I need to do in order to display multiple records from the XML. I have copied a sample XML that I am using. Thank you.

<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>

Upvotes: 0

Views: 625

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502476

You've only got one cs element, so it's only yielding one element. I think you want this:

// 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"
                          };

EDIT: To find a specific element, I'd use:

var match = coupon.Descendants("c")
                  .Where(c => (string) c.Attribute("mId") == mId)
                  .Single();

Upvotes: 3

Related Questions