This documentation is archived and is not being maintained.

XmlNamespaceManager.AddNamespace Method

Adds the given namespace to the collection.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public virtual void AddNamespace(
	string prefix,
	string uri


Type: System.String

The prefix to associate with the namespace being added. Use String.Empty to add a default namespace.

Note   If the XmlNamespaceManager will be used for resolving namespaces in an XML Path Language (XPath) expression, a prefix must be specified. If an XPath expression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. For more information about XPath expressions and the XmlNamespaceManager, refer to the XmlNode.SelectNodes and XPathExpression.SetContext methods.

Type: System.String

The namespace to add.


The value for prefix is "xml" or "xmlns".


The value for prefix or uri is null.

XmlNamespaceManager does not check prefix and uri for conformance.

XmlReader checks names, including prefixes and namespaces, to ensure they are valid XML names according to the World Wide Web Consortium (W3C) specification. XmlNamespaceManager is used internally by XmlReader, so to avoid a duplication of efforts, XmlNamespaceManager assumes all prefixes and namespaces are valid.

If the prefix and namespace already exist within the current scope, the new prefix and namespace pair will replace the existing prefix/namespace combination. The same prefix and namespace combination can exist across different scopes.

The following prefix/namespace pairs are added by default to the XmlNamespaceManager. They can be determined at any scope.



xmlns (the xmlns prefix namespace)

xml (the XML namespace)


String.Empty (the empty namespace). This value can be reassigned a different prefix. For example, xmlns="" defines the default namespace to be the empty namespace

The following example uses XmlNamespaceManager to resolve namespaces in an XML fragment.

using System;
using System.Xml;

public class Sample

    public static void Main()

        XmlTextReader reader = null;


            // Create the string containing the XML to read.
            String xmlFrag = "<book>" +
                           "<title>Pride And Prejudice</title>" +
                           "<author>" +
                           "<first-name>Jane</first-name>" +
                           "<last-name>Austen</last-name>" +
                           "</author>" +
                           "<curr:price>19.95</curr:price>" +
                           "<misc>&h;</misc>" +

            // Create an XmlNamespaceManager to resolve namespaces.
            NameTable nt = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
            nsmgr.AddNamespace(String.Empty, "urn:samples"); //default namespace
            nsmgr.AddNamespace("curr", "urn:samples:dollar");

            // Create an XmlParserContext.  The XmlParserContext contains all the information 
            // required to parse the XML fragment, including the entity information and the 
            // XmlNamespaceManager to use for namespace resolution.
            XmlParserContext context;
            String subset = "<!ENTITY h 'hardcover'>";
            context = new XmlParserContext(nt, nsmgr, "book", null, null, subset, null, null, XmlSpace.None);

            // Create the reader.
            reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

            // Parse the file and display the node values. 
            while (reader.Read())
                if (reader.HasValue)
                    Console.WriteLine("{0} [{1}] = {2}", reader.NodeType, reader.Name, reader.Value);
                    Console.WriteLine("{0} [{1}]", reader.NodeType, reader.Name);
            if (reader != null)
} // End class
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;

int main() {

    XmlValidatingReader* reader = 0;

    try {

      // Create the string containing the XML to read.
      String* xmlFrag=S"<book>"
                      S"<title>Pride And Prejudice</title>"

      // Create an XmlNamespaceManager to resolve namespaces.
      NameTable* nt = new NameTable();
      XmlNamespaceManager* nsmgr = new XmlNamespaceManager(nt);
      nsmgr->AddNamespace(String::Empty, S"urn:samples"); //default namespace
      nsmgr->AddNamespace(S"curr", S"urn:samples:dollar");

      // Create an XmlParserContext.  The XmlParserContext contains all the information
      // required to parse the XML fragment, including the entity information and the
      // XmlNamespaceManager to use for namespace resolution.
      XmlParserContext* context;
      String* subset=S"<!ENTITY h 'hardcover'>";
      context=new XmlParserContext(nt, nsmgr, S"book", 0, 0, subset, 0, 0, XmlSpace::None);

      // Create the reader.
      reader = new XmlValidatingReader(xmlFrag, XmlNodeType::Element, context);
      reader->ValidationType = ValidationType::None;

      // Parse the file and display the node values.
      while(reader->Read()) {
        if (reader->HasValue)
          Console::WriteLine(S"{0} [{1}] = {2}", __box(reader->NodeType),reader->Name,reader->Value);
          Console::WriteLine(S"{0} [{1}]", __box(reader->NodeType),reader->Name);
    __finally {
      if (reader != 0)

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0