title: Why is XmlNamespaceManager necessary?
body: <p>I've come up kinda dry as to <strong>why</strong> -- at least in the .Net Framework -- it is necessary to use an <code>XmlNamespaceManager</code> in order to handle namespaces (or the rather clunky and verbose <code>[local-name()=...</code> XPath predicate/function/whatever) when performing XPath queries. I <strong>do</strong> understand why namespaces are necessary or at least beneficial, but <strong>why</strong> is it so complex?</p>
  
  <p>In order to query a simple XML Document (no namespaces)...</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;rootNode&gt;
     &lt;nodeName&gt;Some Text Here&lt;/nodeName&gt;
  &lt;/rootNode&gt;
  </code></pre>
  
  <p>...one can use something like <code>doc.SelectSingleNode("//nodeName")</code> (which would match <code>&lt;nodeName&gt;Some Text Here&lt;/nodeName&gt;</code>)</p>
  
  <p><em>Mystery #1</em>: <strong>My first annoyance</strong> -- If I understand correctly -- is that merely adding a namespace reference to the parent/root tag (whether used as part of a child node tag or not) like so:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;rootNode xmlns="http://example.com/xmlns/foo"&gt;
     &lt;nodeName&gt;Some Text Here&lt;/nodeName&gt;
  &lt;/rootNode&gt;
  </code></pre>
  
  <p>...requires several extra lines of code to get the same result:</p>
  
  <pre><code>Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
  nsmgr.AddNamespace("ab", "http://example.com/xmlns/foo")
  Dim desiredNode As XmlNode = doc.SelectSingleNode("//ab:nodeName", nsmgr)
  </code></pre>
  
  <p>...essentially dreaming up a non-existent prefix ("<code>ab</code>") to find a node that doesn't even use a prefix. <em>How does this make sense?</em> What is wrong (conceptually) with <code>doc.SelectSingleNode("//nodeName")</code>?</p>
  
  <p><em>Mystery #2</em>: So, say you've got an XML document that uses prefixes:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;rootNode xmlns:cde="http://example.com/xmlns/foo" xmlns:feg="http://example.com/xmlns/bar"&gt;
     &lt;cde:nodeName&gt;Some Text Here&lt;/cde:nodeName&gt;
     &lt;feg:nodeName&gt;Some Other Value&lt;/feg:nodeName&gt;
     &lt;feg:otherName&gt;Yet Another Value&lt;/feg:otherName&gt;
  &lt;/rootNode&gt;
  </code></pre>
  
  <p>... If I understand correctly, you would have to add both namespaces to the <code>XmlNamespaceManager</code>, in order to make a query for a single node...</p>
  
  <pre><code>Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
  nsmgr.AddNamespace("cde", "http://example.com/xmlns/foo")
  nsmgr.AddNamespace("feg", "http://example.com/xmlns/bar")
  Dim desiredNode As XmlNode = doc.SelectSingleNode("//feg:nodeName", nsmgr)
  </code></pre>
  
  <p>... Why, in this case, do I need (conceptually) a namespace manager? </p>
  
  <p>******REDACTED into comments below****</p>
  
  <p><strong>Edit Added:</strong>
  My revised and refined question is based upon the apparent redundancy of the XmlNamespaceManager in what I believe to be the majority of cases and the use of the namespace manager to specify a mapping of prefix to URI:</p>
  
  <p>When the direct mapping of the namespace prefix ("cde") to the namespace URI ("<a href="http://example.com/xmlns/foo" rel="noreferrer">http://example.com/xmlns/foo</a>") is explicitly stated in the source document: </p>
  
  <pre><code>...&lt;rootNode xmlns:cde="http://example.com/xmlns/foo"...
  </code></pre>
  
  <p>what is the conceptual need for a programmer to recreate that mapping before making a query?</p>