XmlNamespaceManager.LookupPrefix Method

Finds the prefix declared for the given namespace URI.

Namespace:  System.Xml
Assemblies:   System.Xml (in System.Xml.dll)
  System.Xml.ReaderWriter (in System.Xml.ReaderWriter.dll)

public virtual string LookupPrefix(
	string uri


Type: System.String

The namespace to resolve for the prefix.

Return Value

Type: System.String
The matching prefix. If there is no mapped prefix, the method returns String.Empty. If a null value is supplied, then null is returned.



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.

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:

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")
s = writer.LookupPrefix("book")
CError.Compare(s, Nothing, "Error in root element")


<root xmlns="book">
  <x:node xmlns:x="author" /> 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft