Share via


Attribute and Namespace Navigation Using XPathNavigator

Attributes and namespace nodes cannot be navigated to using the Move methods that are used in navigating a node set tree. Instead, there are methods specific to navigating just attributes and namespaces. For attribute navigation, use the following methods.

  • MoveToAttribute
  • MoveToFirstAttribute
  • MoveToNextAttribute

To navigate through namespaces, use the following methods.

  • MoveToNamespace
  • MoveToFirstNamespace
  • MoveToNextNamespace

Attributes and namespace also have methods that return their value, the GetAttribute and GetNamespace methods.

There are two node set navigation methods that work on all node types, including attributes and namespaces. They are:

  • MoveToRoot
  • MoveTo

If any Move method listed in Node Set Navigation using XPathNavigator is called on a node set navigator, and the navigator is positioned on an attribute or namespace node, the method returns false and there is no change to the position of the navigator.

When navigating among namespaces, the MoveToFirstNamespace and MoveToNextNamespace methods can also be called with an XPathNamespaceScope parameter. Because of this XPathNamespaceScope parameter, these methods behave differently than their counterparts that are called with no parameters. This XPathNamespaceScope parameter has values of All, ExcludeXml, or Local.

To allow a subset of namespaces to be used for performance reasons, the namespace methods MoveToFirstNamespace and MoveToNextNamespace take an XPathNamespaceSet parameter. This restricts the Move methods to the namespaces specified by the XPathNamespaceSet parameter.

Input

<root xmlns="defaultns" xmlns:a="A" xmlns:b="B">
    <element1  xmlns:a="Z">
        <element2 xmlns:c="C"/>
    </element1>
</root>

Using the XPathNamespaceScope as a parameter to the MoveToFirstNamespace method causes different results to be returned. You must be positioned on an element node before calling the method.

  • MoveToFirstNamespace(XPathNamespaceScope) using All enumeration:

    Works the same as the MoveToFirstNamespace method. The method, if called, returns one of the valid namespaces: xmlns:c="C", xmlns:a="Z", xmlns:b="B", , and xmlns:xml="http://www.w3.org/2000/xmlns/". Since no order is implied in the namespaces, it is unknown which of the namespaces will be returned to the method call.

    The MoveToFirstNamespace method returns false when there is no first namespace node to navigate to. However, the MoveToFirstNamespace method with the All XPathNamespaceScope always returns true, as there is always at least one xmlns:xml namespace because of the default namespace that is implicitly declared.

  • MoveToFirstNamespace(XPathNamespaceScope) using ExcludeXml enumeration:

    Does not return xmlns:xml namespace. It returns one of the following namespaces: xmlns:c="C", xmlns:a="Z", , xmlns:b="B".

  • MoveToFirstNamespace(XPathNamespaceScope) using Local enumeration:

    Returns only the namespace nodes that were defined with the element, so returns only xmlns:c="C".

Using the XPathNamespaceScope as a parameter to the MoveToNextNamespace method returns different results against the input, with behavior similar to that described for the MoveToFirstNamespace method. The method returns false when there are no more namespace nodes to navigate to.

Calling MoveToParent after calling either variation of the MoveToFirstNamespace methods takes you back to the same node.

See Also

XPathNavigator in the .NET Framework | Node Types Recognized with XPath Queries | Node Set Navigation Using XPath Queries | Compile, Select, Evaluate, and Matches with XPath and XPathExpressions