XmlNamespaceManager 类


解析集合的命名空间、向集合添加命名空间和从集合中移除命名空间,以及提供对这些命名空间的范围管理。Resolves, adds, and removes namespaces to a collection and provides scope management for these namespaces.

public ref class XmlNamespaceManager : System::Collections::IEnumerable, System::Xml::IXmlNamespaceResolver
public ref class XmlNamespaceManager : System::Collections::IEnumerable
public class XmlNamespaceManager : System.Collections.IEnumerable, System.Xml.IXmlNamespaceResolver
public class XmlNamespaceManager : System.Collections.IEnumerable
type XmlNamespaceManager = class
    interface IEnumerable
    interface IXmlNamespaceResolver
type XmlNamespaceManager = class
    interface IEnumerable
type XmlNamespaceManager = class
    interface IXmlNamespaceResolver
    interface IEnumerable
Public Class XmlNamespaceManager
Implements IEnumerable, IXmlNamespaceResolver
Public Class XmlNamespaceManager
Implements IEnumerable


有关如何在 XML 文档中声明和使用命名空间的一般信息,请参阅 管理 Xml 文档中的命名空间For general information about how namespaces are declared and used in XML documents, see Managing Namespaces in an XML Document.

XmlNamespaceManager 将前缀和命名空间存储为字符串。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.

功能To 使用Use
添加命名空间Add a namespace AddNamespace 方法AddNamespace method
移除命名空间Remove a namespace RemoveNamespace 方法RemoveNamespace method
查找默认命名空间的 URIFind the URI for the default namespace DefaultNamespace 属性DefaultNamespace property
查找命名空间前缀的 URIFind the URI for a namespace prefix LookupNamespace 方法LookupNamespace method
查找命名空间 URI 的前缀Find the prefix for a namespace URI LookupPrefix 方法LookupPrefix method
获取当前节点中命名空间的列表Get a list of namespaces in the current node GetNamespacesInScope 方法GetNamespacesInScope method
限定命名空间的范围Scope a namespace PushScopePopScope 方法PushScope and PopScope methods
检查是否在当前范围内定义了前缀Check whether a prefix is defined in the current scope HasNamespace 方法HasNamespace method
获取用于查找前缀和 URI 的名称表Get the name table used to look up prefixes and URIs NameTable 属性NameTable property

若要将命名空间添加到命名空间管理器,请创建一个 XmlNamespaceManager 对象,然后使用 AddNamespace 方法。To add namespaces to the namespace manager, you create a XmlNamespaceManager object and then use the AddNamespace method. 创建时默认前缀和命名空间对自动添加到命名空间管理器。Default prefix and namespace pairs are automatically added to the namespace manager on creation.

创建命名空间管理器时,可以从、或类指定名称表 XmlReader XsltContext XmlDocument ,然后使用 AddNamespace 方法添加命名空间。When you create the namespace manager, you can specify a name table from the XmlReader, XsltContext, or XmlDocument class, and then use the AddNamespace method to add the namespaces.

可以将 XmlNamespaceManager 对象作为参数提供给 SelectNodes 类的或 SelectSingleNode 方法, XmlDocument 以执行引用命名空间限定的元素和属性名称的 XPath 查询表达式。You can supply the XmlNamespaceManager object as a parameter to the SelectNodes or SelectSingleNode method of the XmlDocument class to execute XPath query expressions that reference namespace-qualified element and attribute names.

命名空间管理器假定前缀和命名空间已经过验证并符合 W3C 命名空间 规范。The namespace manager assumes that prefixes and namespaces have already been verified and conform to the W3C Namespaces specification. 命名空间管理器不执行任何验证。The namespace manager does not perform any validation.

命名空间管理器通过使用 AddNamespace 方法并在使用或方法执行查找时,原子化字符串 LookupNamespace LookupPrefixThe namespace manager atomizes the strings when they are added by using the AddNamespace method and when a lookup is performed by using the LookupNamespace or LookupPrefix method.

命名空间管理器除了添加和检索命名空间外,还实现枚举支持。The namespace manager implements enumeration support in addition to adding and retrieving namespaces. 可以使用构造循环遍历命名空间管理器中保存的信息 foreachYou can loop through the information saved in the namespace manager by using the foreach construct. 例如,如果使用名称创建命名空间管理器 nsmanager ,则可以通过使用循环访问表 foreach (String prefix in nsmanager)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.

下面的代码示例演示如何将前缀与的 xsd 命名空间 URI 绑定 http://www.w3.org/2001/XMLSchema ,并将其添加到命名空间管理器: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:

nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")  
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");  

然后,可以使用方法找到命名空间 LookupNamespaceYou can then find the namespace by using the LookupNamespace method:


下面的示例 XmlNamespaceManager 使用 XML 读取器中的名称表创建一个:The following example creates an XmlNamespaceManager by using the name table from an XML reader:

Dim reader As New XmlTextReader("myfile.xml")  
Dim nsmanager As New XmlNamespaceManager(reader.NameTable)  
nsmanager.AddNamespace("msbooks", "www.microsoft.com/books")  
nsmanager.AddNamespace("msstore", "www.microsoft.com/store")  
While reader.Read()  
    Console.WriteLine("Reader Prefix:{0}", reader.Prefix)  
    Console.WriteLine("XmlNamespaceManager Prefix:{0}",  
End While  
XmlTextReader reader = new XmlTextReader("myfile.xml");  
XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);  
nsmanager.AddNamespace("msbooks", "www.microsoft.com/books");  
nsmanager.AddNamespace("msstore", "www.microsoft.com/store");  
while (reader.Read())  
    Console.WriteLine("Reader Prefix:{0}", reader.Prefix);  
    Console.WriteLine("XmlNamespaceManager Prefix:{0}",  



使用指定的 XmlNamespaceManager 初始化 XmlNameTable 类的新实例。Initializes a new instance of the XmlNamespaceManager class with the specified XmlNameTable.



获取默认命名空间的命名空间 URI。Gets the namespace URI for the default namespace.


获取与此对象关联的 XmlNameTableGets the XmlNameTable associated with this object.


AddNamespace(String, String)

将给定的命名空间添加到集合。Adds the given namespace to the collection.


确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

返回一个枚举数,将使用该枚举数循环访问 XmlNamespaceManager 集合中的命名空间。Returns an enumerator to use to iterate through the namespaces in the XmlNamespaceManager.


作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取被可用于枚举当前范围内的命名空间的前缀键控的命名空间名称的集合。Gets a collection of namespace names keyed by prefix which can be used to enumerate the namespaces currently in scope.


获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

获取一个值,该值指示所提供的前缀是否具有为当前推送的范围定义的命名空间。Gets a value indicating whether the supplied prefix has a namespace defined for the current pushed scope.


获取指定前缀的命名空间 URI。Gets the namespace URI for the specified prefix.


查找为给定的命名空间 URI 声明的前缀。Finds the prefix declared for the given namespace URI.


创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

将命名空间范围弹出堆栈。Pops a namespace scope off the stack.


将命名空间范围推送到堆栈上。Pushes a namespace scope onto the stack.

RemoveNamespace(String, String)

为给定的前缀移除给定的命名空间。Removes the given namespace for the given prefix.


返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)



IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.


根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.


启用查询的并行化。Enables parallelization of a query.


IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.