Information Obtained from the XmlNamespaceManager

The following table shows that once information has been placed in the XmlNamespaceManager, there are several methods and properties available to perform tasks that set or retrieve information.

Task

Methods or Properties

Finds the namespaceURI defined as the default namespace.

DefaultNamespace Property

Finds the namespaceURI declared and in scope for a prefix.

LookupNamespace Method

Finds the prefix that is declared and in scope for a namespaceURI.

LookupPrefix Method

Adds additional namespaces to XmlNamespaceManager.

AddNamespace Method

Removes namespaces from XmlNamespaceManager.

RemoveNamespace Method

Scopes a namespace.

PushScope Method, PopScope Method

Checks if a prefix is defined in the current scope.

HasNamespace Method

Gets the XmlNameTable associated with an XmlNamespaceManager object.

NameTable Property

The following example shows how the LookupPrefix method is used when writing an attribute. It uses the WriteStartAttribute method to start the attribute, and the LookupPrefix is used to look up the prefix for the urn:samples namespaceURI, then uses that prefix in the WriteStartAttribute when writing the ISBN attribute:

Dim prefix As String = nsMgr.LookupPrefix("urn:samples")
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples")
XmlNameTable nt = new XmlNameTable();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nt);
nsMgr.AddNamespace("bk","urn:samples");string prefix = writer nsMgr.LookupPrefix("urn:samples");
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");

The following example uses LookupPrefix to find the prefix defined on an element.

writer.WriteStartElement("root", "book")
writer.WriteStartElement("x", "node", "author")
s = writer.LookupPrefix("author")
CError.Compare(s, "x", "Error in nested element")
writer.WriteEndElement()
s = writer.LookupPrefix("book")
CError.Compare(s, Nothing, "Error in root element")
writer.WriteEndElement()
writer.WriteStartElement("root", "book");
writer.WriteStartElement("x", "node", "author");
s = writer.LookupPrefix("author");
CError.Compare(s, "x", "Error in nested element");
writer.WriteEndElement();
s = writer.LookupPrefix("book");
CError.Compare(s, null, "Error in root element");
writer.WriteEndElement();

Output

<root xmlns="book">
  <x:node xmlns:x="author" /> 
</root>

The following example shows how to add a namespace using the AddNamespace method, and how to give it specific scope using the PushScope and PopScope methods.

Public Sub Main()
        Dim nametable As NameTable = New NameTable()
        Dim nsm As XmlNamespaceManager = New XmlNamespaceManager(nametable)
        ' Add a default namespace.
        nsm.AddNamespace(String.Empty, "www.microsoft.com")
        nsm.AddNamespace("msbooks", "www.microsoft.com/books")
        'Push the namespace scope on the stack.
        nsm.PushScope()
        nsm.AddNamespace("msstore", "www.microsoft.com/store")
        checkns(nsm)
        nsm.PopScope()
        checkns(nsm)

        Dim response As String = Console.ReadLine

End Sub

Public Sub checkns(ByVal nsm As XmlNamespaceManager)
        If nsm.HasNamespace(String.Empty) Then
            Console.WriteLine("DefaultNamespace in scope")
        Else
            Console.WriteLine("DefaultNamespace not in scope")
        End If
        If nsm.HasNamespace("msstore") Then
            Console.WriteLine("www.microsoft.com/store"" in scope")
        Else
            Console.WriteLine("www.microsoft.com/store not in scope")
        End If
        If nsm.HasNamespace("msbooks") Then
            Console.WriteLine("www.microsoft.com/books in scope")
        Else
            Console.WriteLine("www.microsoft.com/books not in scope")
        End If
End Sub
 
XmlNameTable nt = new XmlNameTable();
XmlNamespaceManager nsmanager = new XmlNamespaceManager(nt);
//Add a default namespace.
nsmanager.AddNamespace (String.Empty, "www.microsoft.com"); 
nsmanager.AddNamespace ("msbooks", "www.microsoft.com/books");
nsmanager.PushScope();
nsmanager.AddNamespace ("msstore", "www.microsoft.com/store");
checkns(nsmanager);
nsmanager.PopScope();
checkns(nsmanager);

checkns(XmlNamespaceManager nsm)
{
    if (nsm.HasNamespace(String.Empty))
        Console.WriteLine("DefaultNamespace in scope");
    else
        Console.WriteLine("DefaultNamespace not in scope");
    if (nsm.HasNamespace("msstore"))
        Console.WriteLine(""www.microsoft.com/store"" in scope");
    else
        Console.WriteLine(""www.microsoft.com/store"" not in scope");
    if (nsm.HasNamespace("msbooks"))
        Console.WriteLine(""www.microsoft.com/books"" in scope");
    else
        Console.WriteLine(""www.microsoft.com/books"" not in scope");
}

Output

DefaultNamespace not in scope
www.microsoft.com/store" in scope
www.microsoft.com/books not in scope
DefaultNamespace in scope
www.microsoft.com/store not in scope
www.microsoft.com/books in scope

The following example enumerates all of the prefixes in the current scope, which also lists the additional three namespaces and prefix pairs. These three default namespace and prefix pairs are automatically added to the namespace manager on creation.

Dim nametable As NameTable = New NameTable()
Dim nsm As XmlNamespaceManager = New XmlNamespaceManager(nametable)
'Add a default namespace.
nsm.AddNamespace("xmlabc", "www.microsoft.com")
Dim prefix As String
For Each prefix In nsm
    Console.WriteLine("Namespace:{0}, Prefix:{1}", nsm.LookupNamespace(nsm.NameTable.Get(prefix)), prefix)
Next
NameTable nametable = new NameTable();
XmlNamespaceManager nsm = new XmlNamespaceManager(nametable);
//Add a default namespace.
nsm.AddNamespace("xmlabc", "www.microsoft.com"); 
foreach (String prefix in nsm)
    {
        Console.WriteLine("Namespace:{0}, Prefix:{1}",nsm.LookupNamespace(nsm.NameTable.Get(prefix)), prefix);
    }

Output

Namespace:http://www.w3.org/XML/1998/namespace, Prefix:xml
Namespace:http://www.w3.org/2000/xmlns/, Prefix:xmlns 
Namespace:, Prefix: 
Namespace:www.microsoft.com, Prefix:xmlabc

See Also

Concepts

Namespaces in an XML Document

Namespace Declarations

Default Namespaces

Namespace Declaration Scope

Manage Namespaces Using the XmlNamespaceManager