Manage Namespaces Using the XmlNamespaceManager

The XmlNamespaceManager class holds a collection of namespace URIs and their prefixes. It allows you to resolve, add, and remove namespaces from a collection. It is required in certain contexts to improve XML processing performance. For instance, the XmlNamespaceManager is used by the XsltContext class for XPath support. For more information on the XsltContext class, see Microsoft Knowledge Base 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.

Note

If you are using LINQ to XML, the XmlNamespaceManager is not used to manage namespaces. See Working with XML Namespaces for information on managing namespaces when using LINQ to XML.

Namespaces

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/XML/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. When creating the namespace manager, you can use the NameTable from the XmlTextReader, XsltContext, or XmlDocument classes. A custom XmlNamespaceManager object is created and filled with the appropriate namespace declarations using the AddNamespace method. The XmlNamespaceManager is supplied as a parameter to the SelectNodes or 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 conform to the W3C Namespaces specification. The namespace manager does not perform any validation on the namespace.

  • The 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 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).

Because 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")
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");

Example

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

nsmgr.LookupNamespace("xsd")
nsmgr.LookupNamespace("xsd");

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

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
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)));
}

See Also

Concepts

Namespaces in an XML Document