Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método XPathNavigator.MoveToFirstAttribute ()

 

Publicado: octubre de 2016

Cuando se reemplaza en una clase derivada, desplaza XPathNavigator hasta el primer atributo del nodo actual.

Espacio de nombres:   System.Xml.XPath
Ensamblado:  System.Xml (en System.Xml.dll)

public abstract bool MoveToFirstAttribute()

Valor devuelto

Type: System.Boolean

Devuelve true si XPathNavigator se desplaza correctamente al primer atributo del nodo actual; en caso contrario, false. Si es false, no cambia la posición de XPathNavigator.

If the T:System.Xml.XPath.XPathNavigator is not currently positioned on an element, this method returns false and the position of the T:System.Xml.XPath.XPathNavigator does not change.

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

After the T:System.Xml.XPath.XPathNavigator is positioned on an attribute, you can call M:System.Xml.XPath.XPathNavigator.MoveToParent to move to the owner element.

The following example uses the M:System.Xml.XPath.XPathNavigator.MoveToFirstAttribute and M:System.Xml.XPath.XPathNavigator.MoveToNextAttribute methods to display all the attributes for each book in the books.xml file.

XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

// Select all book nodes and display all attributes on each book.
XPathNodeIterator nodes = navigator.SelectDescendants("book", "", false);
while (nodes.MoveNext())
{
    XPathNavigator navigator2 = nodes.Current.Clone();
    navigator2.MoveToFirstAttribute();
    Console.WriteLine("{0} = {1}", navigator2.Name, navigator2.Value);

    while (navigator2.MoveToNextAttribute())
    {
        Console.WriteLine("{0} = {1}", navigator2.Name, navigator2.Value);
    }

    Console.WriteLine();
}

The example takes the books.xml file as an input.


<?xml version="1.0" encoding="utf-8" ?> 
<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

.NET Framework
Disponible desde 1.1
Silverlight
Disponible desde 4.0
Volver al principio
Mostrar: