2

I need help regarding parsing of XML data in windows phone 7. I am looking something similar to the example XMl parsign example But i am facing issue when writing the LINQ query for xml data like

<toursList> 
<tour>
<title>short tour </title>
 <description>the short tour is kinda quick! </description>
 <stop> <title>tabaret hall</title>
 <description>tabaret hall </description>
  <location>
    <latitude>45.424585</latitude>
      <longitude>-75.68608</longitude>
   </location>
</stop>
</tour>
</toursList>";

I would be really grateful for any help provided for parsing multilevel xml doc

Thanks and Regards Surya

Community
  • 1
  • 1
surya
  • 991
  • 1
  • 12
  • 22
  • You've shown what XML you're trying to parse, but not what problems you're running into, where the XML is coming from, or what you've tried so far. That makes it very difficult to help you. – Jon Skeet Dec 12 '10 at 07:55

2 Answers2

2

As Jon says above, your question needs a bit more explaination, but maybe something like the following is what your looking for:

var tours = from tour in toursListElement.Elements("tour")
         select new Tour
         {
              Description = tour.Element("description"),
              Stops = (from stop in tour.Elements("stop")
                      select new Stop
                      {
                           Title = stop.Element("title"),
                           Description = stop.Element("description"),
                           Location = new Location
                                      {
                                           Latitude = stop.Element("location").Element("latitude"),
                                           Longitude = stop.Element("location").Element("longitude")
                                      }
                      }).ToList()
         };
2

Without knowing exactly what you're trying to do it's hard to provide exactly what you want but the following shows a way (there are many more) of accessing all the nodes in the example XML.

var tours = from list in xdoc.Elements("toursList")
            select list.Elements("tour");

var tour = tours.First();

var title = tour.Elements("title").First().Value;

var desc = tour.Elements("description").First().Value;

var stop = tour.Elements("stop").First().Value;

var stopTitle = stop.Elements("title").First().Value;

var stopDescription = stop.Elements("description").First().Value;

var stopLocation = stop.Elements("location").First().Value;

var stopLat = stopLocation.Elements("latitude").First().Value;

var stopLong = stopLocation.Elements("longitude").First().Value;
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143