Resolves, adds, and removes namespaces to a collection and provides scope management for these namespaces.
See Also: XmlNamespaceManager Members
For general information about how namespaces are declared and used in XML documents, see Managing Namespaces in an XML Document.
System.Xml.XmlNamespaceManager stores prefixes and namespaces as strings. Here's a summary of management and lookup tasks you can perform with this class. For more information and examples, follow the links to the reference page for each method or property.
Add a namespace | |
Remove a namespace | |
Find the URI for the default namespace |
XmlNamespaceManager.DefaultNamespace property |
Find the URI for a namespace prefix | |
Find the prefix for a namespace URI | |
Get a list of namespaces in the current node |
XmlNamespaceManager.GetNamespacesInScope(XmlNamespaceScope) method |
Scope a namespace |
XmlNamespaceManager.PushScope and XmlNamespaceManager.PopScope methods |
Check whether a prefix is defined in the current scope | |
Get the name table used to look up prefixes and URIs |
XmlNamespaceManager.NameTable property |
To add namespaces to the namespace manager, you create a System.Xml.XmlNamespaceManager object and then use the XmlNamespaceManager.AddNamespace(string, string) method. Default prefix and namespace pairs are automatically added to the namespace manager on creation.
When you create the namespace manager, you can specify a name table from the System.Xml.XmlReader, System.Xml.Xsl.XsltContext, or System.Xml.XmlDocument class, and then use the XmlNamespaceManager.AddNamespace(string, string) method to add the namespaces.
You can supply the System.Xml.XmlNamespaceManager object as a parameter to the erload:System.Xml.XmlNode.SelectNodes or erload:System.Xml.XmlNode.SelectSingleNode method of the System.Xml.XmlDocument class to execute XPath query expressions that reference namespace-qualified element and attribute names.
The namespace manager assumes that prefixes and namespaces have already been verified and conform to the tp://www.w3.org/TR/REC-xml-names/ specification. The namespace manager does not perform any validation.
The namespace manager atomizes the strings when they are added by using the XmlNamespaceManager.AddNamespace(string, string) method and when a lookup is performed by using the XmlNamespaceManager.LookupNamespace(string) or XmlNamespaceManager.LookupPrefix(string) method.
The namespace manager implements enumeration support in addition to adding and retrieving namespaces. You can loop through the information saved in the namespace manager by using the foreach construct. For example, if you create a namespace manager with the name nsmanager, you can iterate through the table by using foreach (String prefix in nsmanager).
Because the namespace manager provides a string comparison with the prefix and namespaces as objects, there is a performance improvement when using the namespace manager over the direct comparison of a string.
The following code example shows how to bind the prefix xsd with the namespace URI of http://www.w3.org/2001/XMLSchema and add it to the namespace manager:
Example
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
Example
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
You can then find the namespace by using the XmlNamespaceManager.LookupNamespace(string) method:
Example
nsmgr.LookupNamespace("xsd")
Example
nsmgr.LookupNamespace("xsd");
The following example creates an System.Xml.XmlNamespaceManager by using the name table from an XML reader:
Example
Dim reader As New XmlTextReader("myfile.xml") Dim nsmanager As New XmlNamespaceManager(reader.NameTable) nsmanager.AddNamespace("msbooks", "www.microsoft.com/books") nsmanager.PushScope() nsmanager.AddNamespace("msstore", "www.microsoft.com/store") While reader.Read() Console.WriteLine("Reader Prefix:{0}", reader.Prefix) Console.WriteLine("XmlNamespaceManager Prefix:{0}", nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI))) End While
Example
XmlTextReader reader = new XmlTextReader("myfile.xml"); XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable); nsmanager.AddNamespace("msbooks", "www.microsoft.com/books"); nsmanager.PushScope(); nsmanager.AddNamespace("msstore", "www.microsoft.com/store"); while (reader.Read()) { Console.WriteLine("Reader Prefix:{0}", reader.Prefix); Console.WriteLine("XmlNamespaceManager Prefix:{0}", nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI))); }