語言

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 抬頭看。

傳回

包含命名空間前綴的 A 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。

適用於

另請參閱