The Elements method returns IEnumerable. Therefore, your let variables point to a sequence of elements, not a single element. You must take the returned single element, which will be an XElement, and then take its Value property to get the concatenated text of its contents. (According to the documentation)
Instead
select new { Name = name, Street = street, City = city }
You must write:
select new { Name = name.Single().Value, Street = street.Single().Value, City = city.Single().Value }
Either there, or directly in let expressions. You can also find a helper method:
public static string StringValueOfElementNamed(XElement node, string elementName) { return node.Elements(elementName).Single().Value; }
Turn this helper method into an extension method if you want to use membership access syntax.
Edit: After reading concurrent answers, the best way to use would be:
public static string StringValueOfElementNamed(XElement node, string elementName) { return node.Element(elementName).Value; }
The element returns the first element found. Beware of the returned null pointer when the item is not found.
source share