XmlNamespaceManager Class
Represents a manager for XML namespaces.
Namespace: System.Xml
Assembly: System.Xml.dll
Summary
The XmlNamespaceManager
class is used to manage XML namespaces within an XML document. It allows you to add, remove, and resolve namespace prefixes and URIs. This is crucial for parsing and processing XML documents that utilize namespaces, ensuring that prefixes are correctly associated with their respective URIs.
Constructor
Public Constructors
XmlNamespaceManager(XmlNameTable nameTable)
Initializes a new instance of the XmlNamespaceManager
class with the specified XmlNameTable
.
Parameters:
Name | Type | Description |
---|---|---|
nameTable |
XmlNameTable | An XmlNameTable to store the names in. |
Properties
Public Properties
NameTable
Gets the XmlNameTable
used by this XmlNamespaceManager
.
Property Value: XmlNameTable. The XmlNameTable
associated with this XmlNamespaceManager
.
Methods
Public Methods
AddNamespace(string prefix, string uri)
Adds a namespace declaration to the XmlNamespaceManager
.
Parameters:
Name | Type | Description |
---|---|---|
prefix |
string | The prefix of the namespace to add. |
uri |
string | The URI of the namespace to add. |
RemoveNamespace(string prefix, string uri)
Removes a namespace declaration from the XmlNamespaceManager
.
Parameters:
Name | Type | Description |
---|---|---|
prefix |
string | The prefix of the namespace to remove. |
uri |
string | The URI of the namespace to remove. |
LookupNamespace(string prefix)
Looks up the given namespace prefix and returns the corresponding URI.
Parameters:
Name | Type | Description |
---|---|---|
prefix |
string | The namespace prefix to look up. |
Returns: string. The URI for the specified prefix, or null
if the prefix is not found.
LookupPrefix(string uri)
Looks up the given namespace URI and returns the corresponding prefix.
Parameters:
Name | Type | Description |
---|---|---|
uri |
string | The namespace URI to look up. |
Returns: string. The prefix for the specified URI, or null
if the URI is not found.
PushScope()
Pushes a new scope onto the namespace manager stack. This is useful for hierarchical processing of XML elements.
PopScope()
Pops the current scope from the namespace manager stack. This reverts to the previous namespace declarations.
HasNamespace(string prefix, string uri)
Checks if the given namespace prefix and URI combination exists in the current scope.
Parameters:
Name | Type | Description |
---|---|---|
prefix |
string | The namespace prefix to check. |
uri |
string | The namespace URI to check. |
Returns: bool. true
if the namespace exists; otherwise, false
.
Usage Example
The following example demonstrates how to use XmlNamespaceManager
to manage namespaces when processing an XML document.
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
string xmlString = @"
Gambardella, Matthew
XML Developer's Guide
Computer
";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
// Create an XmlNamespaceManager
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
// Add namespaces to the manager
nsManager.AddNamespace("bk", "urn:samples:bookstore");
nsManager.AddNamespace("ex", "urn:samples:examples");
// Select nodes using XPath with namespaces
XmlNodeList bookNodes = doc.SelectNodes("//bk:book", nsManager);
foreach (XmlNode bookNode in bookNodes)
{
XmlNode titleNode = bookNode.SelectSingleNode("bk:title", nsManager);
XmlNode genreNode = bookNode.SelectSingleNode("ex:genre", nsManager);
if (titleNode != null && genreNode != null)
{
Console.WriteLine($"Book Title: {titleNode.InnerText}");
Console.WriteLine($"Genre: {genreNode.InnerText}");
}
}
// Look up prefixes and URIs
Console.WriteLine($""\nLookup: bk -> {nsManager.LookupNamespace(""bk"")}"");
Console.WriteLine($""Lookup: urn:samples:bookstore -> {nsManager.LookupPrefix(""urn:samples:bookstore"")}"");
}
}