XmlNamespaceManager.LookupPrefix(String) XmlNamespaceManager.LookupPrefix(String) XmlNamespaceManager.LookupPrefix(String) XmlNamespaceManager.LookupPrefix(String) Method

定義

尋找為指定命名空間 URI 宣告的前置詞。Finds the prefix declared for the given namespace URI.

public:
 virtual System::String ^ LookupPrefix(System::String ^ uri);
public virtual string LookupPrefix (string uri);
abstract member LookupPrefix : string -> string
override this.LookupPrefix : string -> string
Public Overridable Function LookupPrefix (uri As String) As String

參數

uri
String String String String

用來解析前置詞的命名空間。The namespace to resolve for the prefix.

傳回

符合的前置詞。The matching prefix. 如果沒有對應的前置詞,此方法會傳回 String.Empty。If there is no mapped prefix, the method returns String.Empty. 如果提供了 null 值,則會傳回 nullIf a null value is supplied, then null is returned.

實作

範例

下列範例顯示如何在撰寫屬性時LookupPrefix使用方法。The following example shows how to use the LookupPrefix method when writing an attribute. 它會使用XmlWriter.WriteStartAttribute方法來啟動屬性、尋找urn:samples命名空間 URI 的前置詞,然後在寫入ISBN屬性XmlWriter.WriteStartAttribute時,在中使用該前置詞:It uses the XmlWriter.WriteStartAttribute method to start the attribute, looks up the prefix for the urn:samples namespace URI, and then uses that prefix in the XmlWriter.WriteStartAttribute when writing the ISBN attribute:

Dim prefix As String = nsMgr.LookupPrefix("urn:samples")  
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples")  
  
XmlNameTable nt = new XmlNameTable();  
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nt);  
nsMgr.AddNamespace("bk","urn:samples");string prefix = writer nsMgr.LookupPrefix("urn:samples");  
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");  

下列範例會使用LookupPrefix來尋找元素上定義的前置詞。The following example uses LookupPrefix to find the prefix defined on an element.

writer.WriteStartElement("root", "book")  
writer.WriteStartElement("x", "node", "author")  
s = writer.LookupPrefix("author")  
CError.Compare(s, "x", "Error in nested element")  
writer.WriteEndElement()  
s = writer.LookupPrefix("book")  
CError.Compare(s, Nothing, "Error in root element")  
writer.WriteEndElement()  
  

輸出Output

<root xmlns="book">  
  <x:node xmlns:x="author" />   
</root>  

備註

這個方法會藉由流覽堆疊(也就是全域看起來)來尋找對應的前置詞。This method finds the mapped prefix by walking the stack (that is, it looks globally). 提供的字串必須是不可部分完成的,查閱才會成功。The supplied string must be atomized for the lookup to succeed. 換句話說,提供的字串物件必須存在於命名空間管理員的名稱資料表(NameTable)中。In other words, the supplied string object must exist in the namespace manager's name table (NameTable).

傳回的字串也是不可部分完成的。The returned string is also atomized. 如需原子化字串的詳細資訊,請參閱 XmlNameTable 類別。For more information on atomized strings, see the XmlNameTable class.

適用於

另請參閱