XElement.GetPrefixOfNamespace(XNamespace) Method

Definition

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);
public string? GetPrefixOfNamespace (System.Xml.Linq.XNamespace ns);
member this.GetPrefixOfNamespace : System.Xml.Linq.XNamespace -> string
Public Function GetPrefixOfNamespace (ns As XNamespace) As String

Parameters

ns
XNamespace

An XNamespace to look up.

Returns

String

A String that contains the namespace prefix.

Examples

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. 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  

Remarks

This method looks through the XML tree for namespace attributes that are in scope for this element. Namespace prefixes are specified in namespace attributes that are in the XML tree.

If the namespace is the default namespace, and there is no prefix for the namespace, then this method returns null.

Applies to

See also