xml not parsing correctly



I'm trying to parse xml but certain elements aren't returning anything. the first block returns good data but the artist nodes and venue nodes all return null. im not sure what im doing wrong.



XDocument xml = XDocument.Parse(data);

var events = xml.Descendants("events");

foreach(var e in events)
{
foreach(var attribute in e.Descendants("event"))
{
string id = (string)attribute.Element("id");
string Title = (string)attribute.Element("title");
DateTime date = DateTime.Parse(((string)attribute.Element("datetime")), null, DateTimeStyles.RoundtripKind);
string TicketUrl = (string)attribute.Element("ticket_url");
TicketType TicketType = (TicketType)Enum.Parse(typeof(TicketType), (string)attribute.Element("ticket_type"));
TicketAvailability TicketAvailability = (TicketAvailability)Enum.Parse(typeof(TicketAvailability), (string)attribute.Element("ticket_status"));
DateTime TicketSaleDate = DateTime.Parse(((string)attribute.Element("on_sale_datetime")), null, DateTimeStyles.RoundtripKind);
DateTime LastAvailabilityCheck = DateTime.Now;
string FacebookRsvpUrl = (string)attribute.Element("facebook_rsvp_url");
string Description = (string)attribute.Element("description");
List<Artist> artistData = new List<Artist>();
Venue venue = new Venue();

foreach (var artistNode in attribute.Descendants("artist"))
{
var artistName = (string)artistNode.Attribute("name");
var junk = (string)artistNode.Attribute("mbid");
var ImageUrl = (string)artistNode.Attribute("image_url");
var ThumbnailUrl = (string)artistNode.Attribute("thumb_url");

var FaceBookUrl = (string)artistNode.Attribute("facebook_tour_dates_url");

artistData.Add(new Artist(artistName, ThumbnailUrl, ImageUrl, FaceBookUrl));
}

foreach (var venueNode in attribute.Descendants("venue"))
{
var VenueName = (string)venueNode.Element("name");
var City = (string)attribute.Element("city");
var Region = (string)venueNode.Attribute("region");
var Country = (string)venueNode.Attribute("country");
var GpsLocation = (string)venueNode.Attribute("latitude") + ", " + (string)attribute.Attribute("longitude");
venue = new Venue(VenueName, City, Region, Country, GpsLocation);
}


concerts.Add(new Concert(id, artistData, venue, date, TicketSaleDate, Description, FacebookRsvpUrl, Title, TicketUrl, TicketType, TicketAvailability, LastAvailabilityCheck));
}
}


sample data:



<?xml version="1.0" encoding="UTF-8"?>
<events>
<event>
<id>9203898</id>
<title>Fall Out Boy @ DTE Energy Music Theatre in Clarkston, MI</title>
<datetime>2015-07-10T19:00:00</datetime>
<formatted_datetime>Friday, July 10, 2015 at 7:00PM</formatted_datetime>
<formatted_location>Clarkston, MI</formatted_location>
<ticket_url>http://ift.tt/1uK42PK;
<ticket_type>Tickets</ticket_type>
<ticket_status>available</ticket_status>
<on_sale_datetime>2015-01-23T10:00:00</on_sale_datetime>
<facebook_rsvp_url>http://ift.tt/1JnJXBR;
<description></description>
<artists>
<artist>
<name>Fall Out Boy</name>
<mbid>516cef4d-0718-4007-9939-f9b38af3f784</mbid>
<image_url>http://ift.tt/1JnJXBT;
<thumb_url>http://ift.tt/1uK42PO;
<facebook_tour_dates_url>http://bnds.in/1n0W0qQ</facebook_tour_dates_url>
</artist>
</artists>
<venue>
<name>name</name>
<city>place</city>
<region>state</region>
<country>United States</country>
<latitude>142.748783</latitude>
<longitude>-183.380201</longitude>
</venue>
</event>
</events>

No comments:

Post a Comment