XPathNavigator.LocalName Property

Switch View :
ScriptFree
.NET Framework Class Library
XPathNavigator.LocalName Property

Gets the name of the current node without the namespace prefix.

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

Property Value

The local name of the current node. The name returned depends on the NodeType of the current node. For example, the LocalName is book for the <bk:book> element.

XPathNodeType Property Value
Attribute The local name of the attribute.
Element The local name of the element.
ProcessingInstruction The target of the processing instruction.
Namespace The prefix associated with the namespace URI.
All other node types 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 | Name | Prefix