Export (0) Print
Expand All

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
)

Parameters

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

ExceptionCondition
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) 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



                StringBuilder output = new StringBuilder();

                // Create the string containing the XML to read.
                String xmlFrag =
                    @"<root>
                        <data>
	                        <items>
		                        <item id='1'>
		                        </item>
	                        </items>
                        </data>
                        </root>";

                using (XmlReader reader = XmlReader.Create(new StringReader(xmlFrag)))
                {
                    XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);
                    nsmanager.AddNamespace("msbooks", "www.microsoft.com/books");
                    nsmanager.PushScope();
                    nsmanager.AddNamespace("msstore", "www.microsoft.com/store");

                    foreach (String prefix in nsmanager)
                    {
                        output.AppendLine("Prefix" + prefix + " Namespace=" +
                            nsmanager.LookupNamespace(prefix));
                    }
                }
                OutputTextBlock.Text = output.ToString();


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2015 Microsoft