Get attribute values with LINQ to XML I'm pulling the values from the following XML based on the attribute name.  

&lt;DataRow&gt;
  &lt;DataItem name="SYMBOL"&gt;MSFT-US&lt;/DataItem&gt;
  &lt;DataItem name="NAME"&gt;Microsoft Corporation&lt;/DataItem&gt;
  &lt;DataItem name="CUSIP"&gt;59491812311&lt;/DataItem&gt;
  &lt;DataItem name="SEDOL"&gt;2588CFA&lt;/DataItem&gt;
  &lt;DataItem name="CURRENCY"&gt;USD&lt;/DataItem&gt;
  &lt;DataItem name="DATE"&gt;2018-10-05&lt;/DataItem&gt;
  &lt;DataItem name="PRICE"&gt;100.25&lt;/DataItem&gt;
&lt;/DataRow&gt;


Here's my working code to select the values into a list:

IEnumerable&lt;XElement&gt; xDoc = XDocument.Parse(xmlString).Descendants("DataRow");
var query = from item in xDoc
        let symbol = (item.Elements("DataItem")
            .Where(i =&gt; (string)i.Attribute("name") == "SYMBOL").Select(i =&gt; i.Value)).FirstOrDefault()
        let compName = (item.Elements("DataItem")
            .Where(i =&gt; (string)i.Attribute("name") == "NAME").Select(i =&gt; i.Value)).FirstOrDefault()
        let cusip = (item.Elements("DataItem")
            .Where(i =&gt; (string)i.Attribute("name") == "CUSIP").Select(i =&gt; i.Value)).FirstOrDefault()
        let sedol = (item.Elements("DataItem")
            .Where(i =&gt; (string)i.Attribute("name") == "SEDOL").Select(i =&gt; i.Value)).FirstOrDefault()
        select new {
            Symbol = symbol,
            CompName = compName,
            Cusip = cusip,
            Sedol = sedol
        };


I feel that this can be made cleaner.  How can it be done?
If you don't want create a full-blown xml serializer (I wouldn't if it was a simple as that) all you can do is to just tide this up. This is, add a helper variable for dataItems and create a helper extension for finding elements based on the Name attribute. Then you can remove all the other intermediate viariables and directly assign the values to the properties because this is pretty short right now.

public static string ValueWhereName(this IEnumerable&lt;XElement&gt; sournce, string name)
{
    return sournce.SingleOrDefault(i =&gt; i.Attribute("name").Value.Equals(name))?.Value;
}


and use it like this:

var query =
    from item in dataRows
    let dataItems = item.Elements("DataItem")
    select new
    {
        Symbol = dataItems.ValueWhereName("SYMBOL"),
        CompName = dataItems.ValueWhereName("NAME"),
        Cusip = dataItems.ValueWhereName("CUSIP"),
        Sedol = dataItems.ValueWhereName("SEDOL")
    };


If you had a dedicated class for the result you could make it even more robust by using nameof(YourClass.Symbol). But then the properties would have to have the same names as the attributes or you would need to decorate them with your own attributes and use some reflection... You would also need to modify the Equals(name, StringComparison.OrdinalIgnoreCase) call to ignore-case sind the properties obviously wouldn't match the xml attributes casewise.



Another option could be the package Json.NET as it's also able to parse xml but I'm not sure how it could handle the attributes.