Uredi

Deli z drugimi prek


Attribute and Namespace Node Navigation Using XPathNavigator

The XPathNavigator class provides two sets of navigation methods, the first set, found in the Node Set Navigation Using XPathNavigator topic, are used to navigate node sets in an XPathDocument or XmlDocument object. The second set, described in this topic, are used to navigate attribute and namespace nodes in an XPathDocument or XmlDocument object.

Attribute Node Navigation

Attributes are properties of an element, not children of an element. This distinction is important, because of the methods of the XPathNavigator class used to navigate sibling, parent, and child nodes.

For example, the MoveToPrevious and MoveToNext methods are not used to navigate from an element to an attribute or between attributes. Instead, attributes have distinct methods of navigation.

The following are the attribute navigation methods of the XPathNavigator class.

When the current node is an element, you can use the HasAttributes property to see if there are any attributes associated with the element. After it is known that an element has attributes, there are multiple methods for accessing attributes. To retrieve a single attribute from the element, use the GetAttribute method. To move the XPathNavigator to a particular attribute, use the MoveToAttribute method. You can also iterate over each attribute of an element by using the MoveToFirstAttribute method, followed by multiple calls to the MoveToNextAttribute method.

Note

When the XPathNavigator object is positioned on an attribute or namespace node, the MoveToChild, MoveToFirst, MoveToFirstChild, MoveToFollowing, MoveToId, MoveToNext and MoveToPrevious methods always return false, and have no effect on the position of the XPathNavigator. The exceptions are the MoveTo, MoveToParent, and MoveToRoot methods.

Namespace Node Navigation

Each element has an associated set of namespace nodes, one for each distinct namespace prefix that is bound to a namespace URI in scope for the element (including the XML prefix bound to the http://www.w3.org/XML/1998/namespace namespace, which is implicitly declared in every XML document) and one for the default namespace if one is in scope for the element. The element is the parent of each of these namespace nodes; however, a namespace node is not a child of its parent element.

As with attributes, the MoveToPrevious and MoveToNext methods are not used to navigate from an element to a namespace node, or between namespace nodes. Instead, namespace nodes have distinct methods of navigation.

The following are the namespace navigation methods of the XPathNavigator class.

There is always at least one namespace node in scope for any element in an XML document. This is the namespace node with the prefix xml and namespace URI http://www.w3.org/XML/1998/namespace. To retrieve a namespace URI in scope given a particular prefix, use the GetNamespace method. To move the XPathNavigator object to a particular namespace node, use the MoveToNamespace method. You can also iterate over each namespace node in scope for an element by using the MoveToFirstNamespace method followed by multiple calls to the MoveToNextNamespace method.

Note

When the XPathNavigator object is positioned on an attribute or namespace node, the MoveToChild, MoveToFirst, MoveToFirstChild, MoveToFollowing, MoveToId, MoveToNext and MoveToPrevious methods always return false, and have no effect on the position of the XPathNavigator. The exceptions are the MoveTo, MoveToParent, and MoveToRoot methods.

The XPathNamespaceScope Enumeration

When navigating namespace nodes the MoveToFirstNamespace and MoveToNextNamespace methods can be called with an XPathNamespaceScope parameter. These methods behave differently than their counterparts called with no parameters. The XPathNamespaceScope enumeration has values of All, ExcludeXml, or Local.

The following examples show what namespaces are returned by the MoveToFirstNamespace and MoveToNextNamespace methods at various scopes in an XML document.

<root>  
    <element1 xmlns="http://www.contoso.com" xmlns:books="http://www.contoso.com/books">  
        <element2 />  
    </element1>  
</root>  

The namespace sequence (the namespace the XPathNavigator is positioned upon after calling the MoveToFirstNamespace method followed by a series of calls to the MoveToNextNamespace method) is as follows.

  • When positioned on element2: xmlns:books="http://www.contoso.com/books", xmlns="http://www.contoso.com", and xmlns:xml="http://www.w3.org/XML/1998/namespace".

  • When positioned on element1: xmlns:books="http://www.contoso.com/books", xmlns="http://www.contoso.com", and xmlns:xml="http://www.w3.org/XML/1998/namespace".

  • When positioned on root: xmlns:xml="http://www.w3.org/XML/1998/namespace".

Note

The XPathNavigator class returns namespace nodes in reverse document order. Therefore, MoveToFirstNamespace essentially moves to the last namespace node in the current scope.

The following examples show what namespaces are returned by the MoveToFirstNamespace and MoveToNextNamespace methods with the XPathNamespaceScope enumeration specified at various scopes in an XML document.

<root xmlns="http://www.contoso.com" xmlns:a="http://www.contoso.com/a" xmlns:b="http://www.contoso.com/b">  
    <child1 xmlns="" xmlns:a="urn:a">  
        <child2 xmlns:c="urn:c" />  
    </child1>  
</root>  

When positioned on child2, the namespace sequence (the namespace the XPathNavigator is positioned upon after calling the MoveToFirstNamespace method followed by a series of calls to the MoveToNextNamespace method) is as follows.

  • All: xmlns:c="urn:c", xmlns:a="urn:a", xmlns="", xmlns:b="http://www.contoso.com/b", xmlns:a="http://www.contoso.com/a", xmlns="http://www.contoso.com", and xmlns:xml="http://www.w3.org/XML/1998/namespace".

  • ExcludeXml: xmlns:c="urn:c", xmlns:a="urn:a", xmlns="", xmlns:b="http://www.contoso.com/b", xmlns:a="http://www.contoso.com/a", and xmlns="http://www.contoso.com".

  • Local: xmlns:c="urn:c".

Note

The XPathNavigator class returns namespace nodes in reverse document order. Therefore, MoveToFirstNamespace essentially moves to the last namespace node in the current scope.

See also