XPathNavigator.Name Property

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

Gets the qualified name of the current node.

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

Property Value

The qualified name of the current node. The name returned depends on the NodeType of the current node.

XPathNodeType Property Value
Attribute The qualified name of the attribute. In the following example, <book

bk:genre='novel'>, the Name of the attribute node is bk:genre.

Element The qualifed name of the element. In the following example, <bk:book>, the Name of the element node is bk:book.
Namespace The prefix associated with the namespace URI. In the following namespace declaration, xmlns:bk='urn:samples', the Name property returns bk.
ProcessingInstruction The target of the processing instruction. In the following example, <?xml-stylesheet type='text/xsl' href=

'books.xsl'?>, the Name property returns xml-stylesheet.

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 | LocalName | Prefix