Domanda

Sto cercando di far funzionare il mio XPathNavigator (per andare al nome dell'attributo precedente "Nome") con un pezzo di codice allegato ma in qualche modo non vuole muoversi. Né thisNavigator.MoveToFirstAttribute()thisNavigator.MoveToAttribute("NAME", ""); lavori...

Il mio contenuto XML sembra più o meno così

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

Il mio codice:

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

MODIFICARESolo per essere sicuro di aver appena controllato:

string x = thisNavigator.NamespaceURI;

Ed è davvero vuoto, quindi ora sono ancora più confuso.

EDIT2Ok, l'ho risolto, ma una specie di lungo giro. Devo tornare al genitore, quindi di nuovo a questo figlio e poi all'attributo con il nome dato. Se qualcuno sa perché non vuole andare all'attributo dato "all'indietro", sarei grato per la risposta. Ecco il mio codice:

    thisNavigator.MoveToParent();
    thisNavigator.MoveToFirstChild();
    thisNavigator.MoveToAttribute("NAME", "" );
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top