XElement.GetPrefixOfNamespace(XNamespace) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
- ns
- XNamespace
An XNamespace to look up.
A String that contains the namespace prefix.
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
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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: