Find an XElement with a certain attribute name and value with LINQ
Try
XElement result = elm.Descendants("image") .FirstOrDefault(el => el.Attribute("size") != null && el.Attribute("size").Value == "large");if (result != null) { process result.Value ...}
Starting with C#6.0 (VS 2015), you can write:
XElement result = elm.Descendants("image") .FirstOrDefault(el => el.Attribute("size")?.Value == "large");if (result != null) { process result.Value ...}
A non-obvious alternative (as @RandRandom pointed out) is to cast the Attribute to string
:
XElement result = elm.Descendants("image") .FirstOrDefault(el => (string)el.Attribute("size") == "large");if (result != null) { process result.Value ...}
This works, because because of XAttribute Explicit Conversion (XAttribute to String).
you can use XPathSelectElement extension method
var node = elm.XPathSelectElement("descendant::image[@size='large']");if (node!=null){ var path = node.Value;}