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方法來執行查閱時,命名空間管理員會 atomizes 它們。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讀取器使用 name 資料表來建立: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 的淺層複本 (Shallow Copy)。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.

適用於

另請參閱