Question

I am trying to get my XPathNavigator to work ( to go to previous attribute name "NAME") with attached piece of code but somehow it doesn't want to move. Neither thisNavigator.MoveToFirstAttribute() nor thisNavigator.MoveToAttribute("NAME", ""); works...

My xml contents looks more or less less like this

<PARAM NAME="Debug" HINT="Debug" TYPE="Int" VALUE="1" LEVEL="3"/>

My code:

 switch (thisNavigator.Name)
 {
        case "VALUE":
        {
            thisNavigator.MoveToFirstAttribute();
            thisNavigator.MoveToAttribute("NAME", "");
        }

EDIT Just to be sure I have just checked :

string x = thisNavigator.NamespaceURI;

and it's indeed empty so now I am even more confused.

EDIT2 Ok I have solved this but kind of a long way around. I have to go back to parent, then back to this child and then to the attribute with given name. If anyone knows why it doesn't want to go to given attribute 'backwards' I would be grateful for answer. Here's my code:

    thisNavigator.MoveToParent();
    thisNavigator.MoveToFirstChild();
    thisNavigator.MoveToAttribute("NAME", "" );
Was it helpful?

Solution

Ok I have solved this but kind of a long way around. I have to go back to parent, then back to this child and then to the attribute with given name

The provided code:

       thisNavigator.MoveToAttribute("NAME", "" );   

means: if thisNavigator represents an element that has an attribute named "NAME", "move-to" that attribute.

The most-likely reason this is unsuccessful is that thisNavigator doesn't represents an element at all, or represents an element that doesn't have an attribute named "NAME" .

The following excerpt from the MSDN documentation may be helpful:


Remarks


If the XPathNavigator is not currently positioned on an element, this method returns false.

After a successful call to MoveToAttribute, the LocalName, NamespaceURI and Prefix properties reflect the values of the attribute. When the XPathNavigator is positioned on an attribute, the methods MoveToNext, MoveToPrevious, and MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator. Rather, you can call MoveToNextAttribute to move to the next attribute node.

Once positioned on an attribute, you can call MoveToParent to move to the owner element.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top