XmlNamespaceManager XmlNamespaceManager XmlNamespaceManager XmlNamespaceManager Class

定义

解析集合的命名空间、向集合添加命名空间和从集合中移除命名空间,以及提供对这些命名空间的范围管理。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 class XmlNamespaceManager : System.Collections.IEnumerable, System.Xml.IXmlNamespaceResolver
type XmlNamespaceManager = class
    interface IXmlNamespaceResolver
    interface IEnumerable
Public Class XmlNamespaceManager
Implements IEnumerable, IXmlNamespaceResolver
继承
XmlNamespaceManagerXmlNamespaceManagerXmlNamespaceManagerXmlNamespaceManager
派生
实现

注解

有关如何在 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从、 XsltContextXmlDocument类指定名称表,然后使用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对象作为参数提供SelectNodesXmlDocument类的或SelectSingleNode方法,以执行引用命名空间限定的元素和属性名称的 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使用或LookupPrefix方法执行查找时,原子化字符串。The 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. 可以使用foreach构造循环遍历命名空间管理器中保存的信息。You 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与的命名空间http://www.w3.org/2001/XMLSchema URI 绑定,并将其添加到命名空间管理器: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");  

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

nsmgr.LookupNamespace("xsd")  
nsmgr.LookupNamespace("xsd");  

下面的示例使用 XML XmlNamespaceManager读取器中的名称表创建一个: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.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  
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)));  
}  

构造函数

XmlNamespaceManager(XmlNameTable) XmlNamespaceManager(XmlNameTable) XmlNamespaceManager(XmlNameTable) XmlNamespaceManager(XmlNameTable)

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

属性

DefaultNamespace DefaultNamespace DefaultNamespace DefaultNamespace

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

NameTable NameTable NameTable NameTable

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

方法

AddNamespace(String, String) AddNamespace(String, String) AddNamespace(String, String) AddNamespace(String, String)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetNamespacesInScope(XmlNamespaceScope) GetNamespacesInScope(XmlNamespaceScope) GetNamespacesInScope(XmlNamespaceScope) GetNamespacesInScope(XmlNamespaceScope)

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

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
HasNamespace(String) HasNamespace(String) HasNamespace(String) HasNamespace(String)

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

LookupNamespace(String) LookupNamespace(String) LookupNamespace(String) LookupNamespace(String)

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

LookupPrefix(String) LookupPrefix(String) LookupPrefix(String) LookupPrefix(String)

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

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
PopScope() PopScope() PopScope() PopScope()

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

PushScope() PushScope() PushScope() PushScope()

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

RemoveNamespace(String, String) RemoveNamespace(String, String) RemoveNamespace(String, String) RemoveNamespace(String, String)

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

ToString() ToString() ToString() ToString()

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

(Inherited from Object)

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

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

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅