XmlNamespaceManager.LookupPrefix(String) 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.
Finds the prefix declared for the given namespace URI.
public:
virtual System::String ^ LookupPrefix(System::String ^ uri);
public virtual string LookupPrefix (string uri);
public virtual string? LookupPrefix (string uri);
abstract member LookupPrefix : string -> string
override this.LookupPrefix : string -> string
Public Overridable Function LookupPrefix (uri As String) As String
- uri
- String
The namespace to resolve for the prefix.
The matching prefix. If there is no mapped prefix, the method returns String.Empty. If a null value is supplied, then null
is returned.
The following example shows how to use the LookupPrefix method when writing an attribute. It uses the XmlWriter.WriteStartAttribute method to start the attribute, looks up the prefix for the urn:samples
namespace URI, and then uses that prefix in the XmlWriter.WriteStartAttribute when writing the ISBN
attribute:
Dim prefix As String = nsMgr.LookupPrefix("urn:samples")
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples")
XmlNameTable nt = new XmlNameTable();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nt);
nsMgr.AddNamespace("bk","urn:samples");string prefix = writer nsMgr.LookupPrefix("urn:samples");
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
The following example uses LookupPrefix to find the prefix defined on an element.
writer.WriteStartElement("root", "book")
writer.WriteStartElement("x", "node", "author")
s = writer.LookupPrefix("author")
CError.Compare(s, "x", "Error in nested element")
writer.WriteEndElement()
s = writer.LookupPrefix("book")
CError.Compare(s, Nothing, "Error in root element")
writer.WriteEndElement()
Output
<root xmlns="book">
<x:node xmlns:x="author" />
</root>
This method finds the mapped prefix by walking the stack (that is, it looks globally). The supplied string must be atomized for the lookup to succeed. In other words, the supplied string object must exist in the namespace manager's name table (NameTable).
The returned string is also atomized. For more information on atomized strings, see the XmlNameTable class.
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 |
.NET Framework | 1.1, 2.0, 3.0, 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: