語言

XElement.GetNamespaceOfPrefix(String) 方法

定義

取得與特定前綴 XElement相關的命名空間。

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

參數

prefix
String

一個包含命名空間前綴的字串,供查找。

傳回

An XNamespace 代表與此 XElement前綴相關聯的命名空間。

範例

以下範例建立一個具有命名空間及相關前綴的 XML 樹。 接著用此方法取得前綴的 。XNamespace

XElement xmlTree = XElement.Parse("<Root xmlns:aw='http://www.adventure-works.com'/>");
XNamespace awNamespace = xmlTree.GetNamespaceOfPrefix("aw");
Console.WriteLine("Namespace: {0}", awNamespace);

使用Visual Basic時,通常會使用 GetXmlNamespace Operator,如下

Imports <xmlns:aw="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = <aw:Root/>
        Dim awNamespace As XNamespace = GetXmlNamespace(aw)
        Console.WriteLine("Namespace: {0}", awNamespace)
    End Sub
End Module

此範例會產生下列輸出:

Namespace: http://www.adventure-works.com

備註

此方法會從 XML 樹中尋找該元素作用範圍內的命名空間屬性。 命名空間前綴在 XML 樹中的命名空間屬性中指定。

適用於

另請參閱