using LINQ to XML, this is a sample of my XML
<shows>
<Show Code="456" Name="My Event Name">
<Event Code="2453" VenueCode="39" Date="2010-04-13 10:30:00" />
<Event Code="2454" VenueCode="39" Date="2010-04-13 13:30:00" />
<Event Code="2455" VenueCode="39" Date="2010-04-14 10:30:00" />
<Event Code="2456" VenueCode="39" Date="2010-04-14 13:30:00" />
<Event Code="2457" VenueCode="39" Date="2010-04-15 10:30:00" />
</Show>
<Show... />
<Show... />
</shows>
How do I return a list of the Dates for a specfic show? I am passing the show code (“456”) in the querystring and want all the dates/times returned as a list.
This is the code i have so far:
XDocument xDoc = XDocument.Load("path to xml");
var feeds = from feed in xDoc.Descendants("Show")
where feed.Attribute("Code").Equals("456")
select new
{
EventDate = feed.Attribute("Date").Value
};
foreach(var feed in feeds)
{
Response.Write(feed.EventDate + "<br />");
}
But i get no results returned
The attribute isn’t going to equal “456” – it’s an attribute, not a string. However, if you convert it to a string first, it will work.
An alternative would be to
intto make sure it’s numeric:EDIT: Okay, I’ve now got this as a short but complete program to show it working.
Note that your original code will only work if the “Show” element has a “Date” attribute – which it doesn’t in your sample XML. Note that it’s trying to take the “Date” from the “Show” element not the “Event” element. I’m not sure what you really wanted to do here, so I’ve changed the code to just cast to
DateTime?instead. The code below works and prints 1 (i.e. it’s found the single Show element matching the code):If you’re actually trying to find every event date within the show, you need another “from” clause to make it iterate over the events within the show: