This topic has not yet been rated Rate this topic

XPathNavigator.Prefix Property

Gets the prefix associated with the current node.

[Visual Basic]
Public MustOverride ReadOnly Property Prefix As String
[C#]
public abstract string Prefix {get;}
[C++]
public: __property virtual String* get_Prefix() = 0;
[JScript]
public abstract function get Prefix() : String;

Property Value

The prefix associated with the current node. For example, Prefix is bk for the <bk:book> element. If one does not exist, this property returns String.Empty.

Example

[Visual Basic, C#, C++] The following example walks the node tree recursively and displays information on element and text nodes.

[Visual Basic] 
public shared sub RecursiveWalk(nav as XPathNavigator)

   select case nav.NodeType
     case XPathNodeType.Element
        if (nav.Prefix=String.Empty)
          Console.WriteLine("<{0}>", nav.LocalName)
        else
          Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName)
          Console.WriteLine("  "+ nav.NamespaceURI)
        end if
     case XPathNodeType.Text
        Console.WriteLine("  " + nav.Value)
   end select

   if ( nav.MoveToFirstChild() )
      do
         RecursiveWalk(nav)
      loop while ( nav.MoveToNext() )

      nav.MoveToParent()
      if (nav.NodeType = XPathNodeType.Element)
        Console.WriteLine("</{0}>", nav.Name)
      end if
   end if

end sub

[C#] 
public static void RecursiveWalk(XPathNavigator nav)
{
   switch (nav.NodeType){
     case XPathNodeType.Element:
        if (nav.Prefix==String.Empty)
          Console.WriteLine("<{0}>", nav.LocalName);
        else
          Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName);
          Console.WriteLine("\t"+ nav.NamespaceURI);
        break;
     case XPathNodeType.Text:
        Console.WriteLine("\t" + nav.Value);
        break;
    }

    if ( nav.MoveToFirstChild() )
   {
      do{
         RecursiveWalk(nav);
      } while ( nav.MoveToNext() );

      nav.MoveToParent();
      if (nav.NodeType == XPathNodeType.Element)
        Console.WriteLine("</{0}>", nav.Name);
    }
}    

[C++] 
static void RecursiveWalk(XPathNavigator * nav) 
{
   switch (nav -> NodeType) 
   {
   case XPathNodeType::Element:
      if (nav -> Prefix == String::Empty)
         Console::WriteLine(S"< {0}>", nav -> LocalName);
      else
         Console::Write(S"< {0}: {1}>", nav -> Prefix, nav -> LocalName);
      Console::WriteLine(S"\t {0}", nav -> NamespaceURI);
      break;
   case XPathNodeType::Text:
      Console::WriteLine(S"\t {0}", nav -> Value);
      break;
   }

   if (nav -> MoveToFirstChild()) 
   {
      do 
      {
         RecursiveWalk(nav);
      } while (nav -> MoveToNext());

      nav -> MoveToParent();
      if (nav -> NodeType == XPathNodeType::Element)
         Console::WriteLine(S"</ {0}>", nav -> Name);
   }
}    

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XPathNavigator Class | XPathNavigator Members | System.Xml.XPath Namespace

Did you find this helpful?
(2000 characters remaining)