共用方式為


XElement.GetNamespaceOfPrefix Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the namespace associated with a particular prefix for this XElement.

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

Syntax

'Declaration
Public Function GetNamespaceOfPrefix ( _
    prefix As String _
) As XNamespace
public XNamespace GetNamespaceOfPrefix(
    string prefix
)

Parameters

  • prefix
    Type: System.String
    A string that contains the namespace prefix to look up.

Return Value

Type: System.Xml.Linq.XNamespace
An XNamespace for the namespace associated with the prefix for this XElement.

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.

Examples

The following example creates an XML tree that has a namespace with an associated prefix. It then uses this method to retrieve the XNamespace for the prefix.

'add the following line to the the Imports section:
'Imports <xmlns:aw="https://www.adventure-works.com">
Dim output As New StringBuilder
Dim xmlTree As XElement = <aw:Root/>
Dim awNamespace As XNamespace = GetXmlNamespace(aw)
output.Append(String.Format("Namespace: {0}", awNamespace))
output.Append(Environment.NewLine)

OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
XElement xmlTree = XElement.Parse("<Root xmlns:aw='https://www.adventure-works.com'/>");
XNamespace awNamespace = xmlTree.GetNamespaceOfPrefix("aw");
output.Append("Namespace: " + awNamespace + Environment.NewLine);

OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.