This documentation is archived and is not being maintained.

XmlNamespaceManager.AddNamespace Method

Adds the given namespace to the collection.

[Visual Basic]
Public Overridable Sub AddNamespace( _
   ByVal prefix As String, _
   ByVal uri As String _
)
[C#]
public virtual void AddNamespace(
 string prefix,
 string uri
);
[C++]
public: virtual void AddNamespace(
 String* prefix,
 String* uri
);
[JScript]
public function AddNamespace(
   prefix : String,
 uri : String
);

Parameters

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

However, if the XmlNamespaceManager will be used for resolving namespaces in an XPath expression, a prefix must be specified. If an XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. For more information about XPath expressions and the XmlNamespaceManager, refer to the XmlNode.SelectNodes and XPathExpression.SetContext methods.

uri
The namespace to add.

Exceptions

Exception Type Condition
ArgumentException The value for prefix is "xml" or "xmlns".
ArgumentNullException The value for prefix or uri is a null reference (Nothing in Visual Basic).

Remarks

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 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 exists within the current scope, it 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.

Prefix Namespace
xmlns http://www.w3.org/2000/xmlns/ (the xmlns prefix namespace)
xml http://www.w3.org/XML/1998/namespace (The XML namespace)
String.Empty 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

Example

[Visual Basic, C#, C++] The following example uses XmlNamespaceManager to resolve namespaces in an XML fragment.

[Visual Basic] 
Imports System
Imports System.Xml

public class Sample 

  public shared sub Main() 

    Dim reader as XmlValidatingReader = Nothing

    try 

      ' Create the string containing the XML to read.
      Dim xmlFrag as 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>" & _
                     "</book>"  

      ' Create an XmlNamespaceManager to resolve namespaces.
      Dim nt as NameTable = new NameTable()
      Dim nsmgr as XmlNamespaceManager = 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.
      Dim context as XmlParserContext 
      Dim subset as String ="<!ENTITY h 'hardcover'>"
      context=new XmlParserContext(nt, nsmgr, "book", Nothing, Nothing, subset, Nothing, Nothing, 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("{0} [{1}] = {2}",reader.NodeType, reader.Name, reader.Value)
        else
          Console.WriteLine("{0} [{1}]",reader.NodeType, reader.Name)
        end if
      end while
    
    finally 
     if not (reader Is Nothing) then
        reader.Close()
    end if
  end try 
end sub
end class

[C#] 
using System;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlValidatingReader reader = null;

    try {

      // 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>" +
                     "</book>";  

      // 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 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("{0} [{1}] = {2}",reader.NodeType, reader.Name, reader.Value);
        else
          Console.WriteLine("{0} [{1}]",reader.NodeType, reader.Name);
      }
    } 
    finally {
      if (reader != null)
        reader.Close();
    }
  } 
} // End class

[C++] 
#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>"
                      S"<author>"
                      S"<first-name>Jane</first-name>"
                      S"<last-name>Austen</last-name>"
                      S"</author>"
                      S"<curr:price>19.95</curr:price>"
                      S"<misc>&h;</misc>"
                      S"</book>";  

      // 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);
        else
          Console::WriteLine(S"{0} [{1}]", __box(reader->NodeType),reader->Name);
      }
    } 
    __finally {
      if (reader != 0)
        reader->Close();
    }
} 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

XmlNamespaceManager Class | XmlNamespaceManager Members | System.Xml Namespace

Show: