XElement.GetPrefixOfNamespace(XNamespace) 方法

定义

获取与此 XElement 的命名空间关联的前缀。Gets the prefix associated with a namespace for this XElement.

public:
 System::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

要查找的 XNamespaceAn XNamespace to look up.

返回

一个包含命名空间前缀的 StringA String that contains the namespace prefix.

示例

下面的示例创建一个包含带有前缀的命名空间的 XML 树。The following example creates an XML tree that contains a namespace with a prefix. 然后,它使用此方法检索命名空间的前缀。It then uses this method to retrieve the prefix for the namespace. 请注意,在调用此方法时,此示例使用从字符串到 XNamespace 的隐式转换。Notice that this example uses the implicit conversion from string to XNamespace when calling this method.

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  

该示例产生下面的输出:This example produces the following output:

Prefix: aw  

注解

此方法通过 XML 树查找此元素范围内的命名空间属性。This method looks through the XML tree for namespace attributes that are in scope for this element. 命名空间前缀在 XML 树中的命名空间属性中指定。Namespace prefixes are specified in namespace attributes that are in the XML tree.

如果命名空间为默认命名空间,并且没有命名空间的前缀,则此方法返回 null。If the namespace is the default namespace, and there is no prefix for the namespace, then this method returns null.

适用于

另请参阅