XmlNamespaceManager::AddNamespace Method (String^, String^)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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
)

Parameters

prefix
Type: System::String^

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

NoteIf 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.

uri
Type: System::String^

The namespace to add.

Exception Condition
ArgumentException

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

ArgumentNullException

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) Namespaces 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.

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

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

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: