You are using item.Element("location") , which returns the first location item below the item. This is not necessarily the place you were looking for!
I suspect you really want something more:
string location = "Oslo"; var training = from loc in doc.Descendants("location") where loc.Value == location select new { event = loc.Parent.Element("event").Value, event_location = loc.Value };
But then again, what is the meaning of event_location , given that it will always be the location that you passed in the request?
If this is not what you want, please give more detailed information - your question is a little difficult to understand at the moment. The details of what your current code is giving and what you want to give would be useful, as well as what you mean by "name" (in the sense that you really mean "value").
EDIT: Alright, it looks like you want:
string location = "Oslo"; var training = from loc in doc.Descendants("location") where loc.Value == location select new { event = loc.Parent.Element("event").Value, event_locations = loc.Parent.Elements("location") .Select(e => e.Value) };
event_locations will now be a sequence of strings. You can get the desired result:
for (var entry in training) { Console.WriteLine("Event: {0}; Locations: {1}", entry.event, string.Join(", ", entry.event_locations.ToArray()); }
Give it a try and see if you want ...
source share