Manage Namespaces Using the XmlNamespaceManager

XmlNamespaceManager is a class that is designed to resolve, add, remove namespaces from a collection and provide namespace scope management for the namespaces. You can create an XmlNamespaceManager class whenever you want to hold namespaces in a collection, and the collection associates the namespace prefix and their URIs. The XmlNamespacemanager is also used by the XsltContext class for XPath support. For more information on the XsltContextClass, see article Q324899, "HOW TO: Implement and Use Custom Extension Functions when you Execute XPath Queries in Visual Basic .NET".

For the same sample in the C# language, see Microsoft Knowledge Base article Q324462, "HOW TO: Implement and Use Custom Extension Functions when you Execute XPath Queries in Visual C# .NET" in the Microsoft Knowledge Base at https://support.microsoft.com.

The following example creates an XmlNamespaceManager using the NameTable from a reader.

Dim reader As New XmlTextReader("myfile.xml")
Dim nsmanager As New XmlNamespaceManager(reader.NameTable)
nsmanager.AddNamespace("msbooks", "www.microsoft.com/books")
nsmanager.PushScope()
nsmanager.AddNamespace("msstore", "www.microsoft.com/store")
While reader.Read()
    Console.WriteLine("Reader Prefix:{0}", reader.Prefix)
    Console.WriteLine("XmlNamespaceManager Prefix:{0}",
     nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI)))
End While
[C#]
XmlTextReader reader = new XmlTextReader("myfile.xml");
XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);
nsmanager.AddNamespace("msbooks", "www.microsoft.com/books");
nsmanager.PushScope();
nsmanager.AddNamespace("msstore", "www.microsoft.com/store");
while (reader.Read())
{
    Console.WriteLine("Reader Prefix:{0}", reader.Prefix);
    Console.WriteLine("XmlNamespaceManager Prefix:{0}",
    nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI)));
}

When a namespace manager is created, three prefixes are added to the class automatically. The following table lists these three prefixes, and the namespaces they represent.

Prefix Namespace
xmlns http://www.w3.org/2000/xmlns
xml http://www.w3.org/1998/namespace
String.Empty The empty namespace. This value can be assigned to a prefix. For example, defines the default namespace to be the empty namespace.

To add namespaces to the namespace manager, create a namespace manager, and then use the AddNamespace method to add a namespace. When creating the namespace manager, you can use the NameTable from the XmlTextReader or XsltContext classes. You can also use the NameTable from an XmlDocument. A custom NamspaceManager object is created, filled with the appropriate namespace declarations using the AddNamespace method, and supplied as a parameter to the SelectNodes and SelectSingleNode methods of the XmlDocument class to execute XPath query expressions that reference namespace-qualified element and attribute names. The following assumptions are made when namespaces are added:

  • Prefixes and namespaces have already been verified and that they conform to the W3C Namespaces specification. The namespace manager does not perform any validation on the namespace.
  • Namespace manager atomizes the strings when they are added using the AddNamespace method.
  • The namespace manager atomizes the strings when a lookup is performed using the LookupNamespace or LookupPrefix method.
  • Default prefix and namespace pairs are automatically added to the namespace manager on creation.

The namespace manager implements enumeration support, in addition to adding and retrieving namespaces. You can therefore loop through the information saved in the namespace manager using the foreach construct. Assuming that a namespace manager has been created with the name nsmanager, you can iterate through the table using foreach (String prefix in nsmanager).

Since the namespace manager provides a string comparison with the prefix and namespaces as objects, there is a performance improvement when using the namespace manager over the direct comparison of a string.

To add a namespace to the namespace manager, use the AddNamespace method. The following code example shows how to bind the prefix xsd with the namespace URI of http://www.w3.org/2001/XMLSchema:

nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
[C#]
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");

The following code example shows how to then find the namespace using the LookupNamespace method:

nsmgr.LookupNamespace("xsd")
[C#]
nsmgr.LookupNamespace("xsd");

A more complete sample of adding and retrieving namespaces is found at XmlNamespaceManager.LookupNamespace Method.

See Also

Namespaces in an XML Document | Namespace Declarations | Default Namespaces | Namespace Declarations Scope | Information Obtained from the XmlNamespaceManager