C# Get Xelement Attribute Value by Attribute Name
<p>I'm trying to get the value of <code>operating-system</code> attribute in the xml code below but in all of the solutions I've tried so far from stack, dotnetcurry, and Microsoft, I get either a <code>NullPointerExecption</code> or it just returns no values. </p>

<p>Here is the xml data I'm trying to parse:</p>

<pre><code>&lt;Report name="Black Workstation" xmlns:cm="http://www.nessus.org/cm"&gt;
  &lt;ReportHost name="192.168.1.000&gt;
    &lt;HostProperties&gt;
        &lt;tag name="HOST_END"&gt;Wed Jun 29 10:32:54 2016&lt;/tag&gt;
        &lt;tag name="LastAuthenticatedResults"&gt;1467214374&lt;/tag&gt;
        &lt;tag name="patch-summary-total-cves"&gt;5&lt;/tag&gt;
        &lt;tag name="cpe"&gt;cpe:/o:microsoft:windows&lt;/tag&gt;
        &lt;tag name="operating-system"&gt;Microsoft Windows 7 Enterprise Service Pack 1&lt;/tag&gt;
    &lt;/HostProperties&gt;
  &lt;/ReportHost&gt;
&lt;/Report&gt;
</code></pre>

<p>I've tried numerous methods but here are the last two:</p>

<pre><code>XNamespace ns = "http://www.nessus.org/cm";
var operatingSystem = (findings.Where(a =&gt; a.Attribute("name").Value == "operating-system")
.Select(a =&gt; a.Value));                                        

var operatingSystem = findings.Descendants().Where(x =&gt; x.Attribute("name").Value == ns + "operating-system");
</code></pre>

<p>Also tried this solution: <a href="https://stackoverflow.com/q/2340411/1072681">Use Linq to Xml with Xml namespaces</a></p>

<p>I have this method here from the Microsoft tutorial but it only returns true/false and I am not able to manipulate it such that it assigns the value <code>Microsoft Windows 7 Enterprise Service Pack 1</code> to the <code>os</code> variable. </p>

<pre><code>XElement xelement = XElement.Load(s);
IEnumerable&lt;XElement&gt; findings = xelement.Elements();

var hostProperties = from hp in findings.Descendants("HostProperties")
                     select new
                     {
                         os = (string)hp.Element("tag").Attribute("name").Value == "operating-system",
                     };
</code></pre>

<p>I've tried using various other Linq-to-Xml queries with the <code>where</code> clause like above but they all return <code>Null</code> or <code>no values to enumerate</code>. </p>