Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6733701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:48:02+00:00 2026-05-26T10:48:02+00:00

I’ve come up kinda dry as to why — at least in the .Net

  • 0

I’ve come up kinda dry as to why — at least in the .Net Framework — it is necessary to use an XmlNamespaceManager in order to handle namespaces (or the rather clunky and verbose [local-name()=... XPath predicate/function/whatever) when performing XPath queries. I do understand why namespaces are necessary or at least beneficial, but why is it so complex?

In order to query a simple XML Document (no namespaces)…

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode>
   <nodeName>Some Text Here</nodeName>
</rootNode>

…one can use something like doc.SelectSingleNode("//nodeName") (which would match <nodeName>Some Text Here</nodeName>)

Mystery #1: My first annoyance — 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:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode xmlns="http://example.com/xmlns/foo">
   <nodeName>Some Text Here</nodeName>
</rootNode>

…requires several extra lines of code to get the same result:

Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ab", "http://example.com/xmlns/foo")
Dim desiredNode As XmlNode = doc.SelectSingleNode("//ab:nodeName", nsmgr)

…essentially dreaming up a non-existent prefix (“ab“) to find a node that doesn’t even use a prefix. How does this make sense? What is wrong (conceptually) with doc.SelectSingleNode("//nodeName")?

Mystery #2: So, say you’ve got an XML document that uses prefixes:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode xmlns:cde="http://example.com/xmlns/foo" xmlns:feg="http://example.com/xmlns/bar">
   <cde:nodeName>Some Text Here</cde:nodeName>
   <feg:nodeName>Some Other Value</feg:nodeName>
   <feg:otherName>Yet Another Value</feg:otherName>
</rootNode>

… If I understand correctly, you would have to add both namespaces to the XmlNamespaceManager, in order to make a query for a single node…

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)

… Why, in this case, do I need (conceptually) a namespace manager?

******REDACTED into comments below****

Edit Added:
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:

When the direct mapping of the namespace prefix (“cde”) to the namespace URI (“http://example.com/xmlns/foo“) is explicitly stated in the source document:

...<rootNode xmlns:cde="http://example.com/xmlns/foo"...

what is the conceptual need for a programmer to recreate that mapping before making a query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:48:03+00:00Added an answer on May 26, 2026 at 10:48 am

    The basic point (as pointed out by Kev, above), is that the namespace URI is the important part of the namespace, rather than the namespace prefix, the prefix is an “arbitrary convenience”

    As for why you need a namespace manager, rather than there being some magic that works it out using the document, I can think of two reasons.

    Reason 1

    If it were permitted to only add namespace declarations to the documentElement, as in your examples, it would indeed be trivial for selectSingleNode to just use whatever is defined.

    However, you can define namespace prefixes on any element in a document, and namespace prefixes are not uniquely bound to any given namespace in a document. Consider the following example

    <w xmlns:a="mynamespace">
      <a:x>
        <y xmlns:a="myOthernamespace">
          <z xmlns="mynamespace">
          <b:z xmlns:b="mynamespace">
          <z xmlns="myOthernamespace">
          <b:z xmlns:b="myOthernamespace">
        </y>
      </a:x>
    </w>
    

    In this example, what would you want //z, //a:z and //b:z to return? How, without some kind of external namespace manager, would you express that?

    Reason 2

    It allows you to reuse the same XPath expression for any equivalent document, without needing to know anything about the namespace prefixes in use.

    myXPathExpression = "//z:y"
    doc1.selectSingleNode(myXPathExpression);
    doc2.selectSingleNode(myXPathExpression);
    

    doc1:

    <x>
      <z:y xmlns:z="mynamespace" />
    </x>
    

    doc2:

    <x xmlns"mynamespace">
      <y>
    </x>
    

    In order to achieve this latter goal without a namespace manager, you would have to inspect each document, building a custom XPath expression for each one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am using Paperclip to handle profile photo uploads in my app. They upload
string formIdList = 8256, 8258, 8362, 8120, 8270, 8271, 8272, 8273, 8257, 8279, 8212,
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.