title: Getting an XElement with a namespace via XPathSelectElements
body: <p>I have an XML e.g.</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;A1&gt;
         &lt;B2&gt;
            &lt;C3 id="1"&gt;
               &lt;D7&gt;
                  &lt;E5 id="abc" /&gt;
               &lt;/D7&gt;
               &lt;D4 id="1"&gt;
                  &lt;E5 id="abc" /&gt;
               &lt;/D4&gt;
               &lt;D4 id="2"&gt;
                  &lt;E5 id="abc" /&gt;
               &lt;/D4&gt;
            &lt;/C3&gt;
         &lt;/B2&gt;
      &lt;/A1&gt;
  </code></pre>
  
  <p>This is may sample code:</p>
  
  <pre><code>    var xDoc = XDocument.Load("Test.xml");
      string xPath = "//B2/C3/D4";
      //or string xPath = "//B2/C3/D4[@id='1']";
  
      var eleList = xDoc.XPathSelectElements(xPath).ToList();
      foreach (var xElement in eleList)
      {
          Console.WriteLine(xElement);
      }
  </code></pre>
  
  <p>It works perfectly, but if I add a namespace to the root node A1, this code doesn't work.
  Upon searching for solutions, I found <a href="https://stackoverflow.com/questions/4857172/using-xelement-to-query-for-a-node-in-namespace">this one</a>, but it uses the <strong>Descendants()</strong> method to query the XML. From my understanding, this solution would fail if I was searching for &lt;E5&gt; because the same tag exists for &lt;D7&gt;, &lt;D4 id="1"&gt; and &lt;D4 id="2"&gt;</p>
  
  <p>My requirement is to search if a node exists at a particular XPath. If there is a way of doing this using Descendants, I'd be delighted to use it. If not, please guide me on how to search using the name space.</p>
  
  <p>My apologies in case this is a duplicate.</p>