XElement.GetPrefixOfNamespace(XNamespace) 方法

定義

取得與這個 XElement 之命名空間相關聯的前置詞。

public:
 System::String ^ GetPrefixOfNamespace(System::Xml::Linq::XNamespace ^ ns);
public string GetPrefixOfNamespace (System.Xml.Linq.XNamespace ns);
public string? GetPrefixOfNamespace (System.Xml.Linq.XNamespace ns);
member this.GetPrefixOfNamespace : System.Xml.Linq.XNamespace -> string
Public Function GetPrefixOfNamespace (ns As XNamespace) As String

參數

ns
XNamespace

要查閱的 XNamespace

傳回

String

包含命名空間前置詞的 String

範例

下列範例會建立 XML 樹狀結構,其中包含具有前置詞的命名空間。 然後,它會使用這個方法來擷取命名空間的前置詞。 請注意,這個範例會在呼叫這個方法時,使用從字串到 XNamespace 的隱含轉換。

XElement xmlTree = XElement.Parse("<Root xmlns:aw='http://www.adventure-works.com'/>");  
string prefix = xmlTree.GetPrefixOfNamespace("http://www.adventure-works.com");  
Console.WriteLine("Prefix: {0}", prefix);  
Imports <xmlns:aw="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = <aw:Root/>  
        Dim prefix As String = xmlTree.GetPrefixOfNamespace("http://www.adventure-works.com")  
        Console.WriteLine("Prefix: {0}", prefix)  
    End Sub  
End Module  

這個範例會產生下列輸出:

Prefix: aw  

備註

這個方法會透過 XML 樹狀結構查看此專案範圍內命名空間屬性。 命名空間前置詞是在 XML 樹狀結構中的命名空間屬性中指定。

如果命名空間是預設命名空間,而且命名空間沒有前置詞,則此方法會傳回 null。

適用於

另請參閱