XPathNavigator.MoveToFirstAttribute 方法

定义

当在派生类中重写时,将 XPathNavigator 移动到当前节点的第一个属性。

public:
 abstract bool MoveToFirstAttribute();
public abstract bool MoveToFirstAttribute ();
abstract member MoveToFirstAttribute : unit -> bool
Public MustOverride Function MoveToFirstAttribute () As Boolean

返回

Boolean

如果 XPathNavigator 成功地移动到当前节点的第一个属性,则为 true;否则为 false。 如果为 false,则 XPathNavigator 的位置不变。

示例

以下示例使用 MoveToFirstAttributeMoveToNextAttribute 方法显示文件中每个书籍 books.xml 的所有属性。

  XPathDocument^ document = gcnew 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();
  }
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();
}
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' Select all book nodes and display all attributes on each book.
Dim nodes As XPathNodeIterator = navigator.SelectDescendants("book", "", False)
While nodes.MoveNext()
    Dim navigator2 As XPathNavigator = nodes.Current.Clone()
    navigator2.MoveToFirstAttribute()
    Console.WriteLine("{0} = {1}", navigator2.Name, navigator2.Value)

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

    Console.WriteLine()
End While

该示例使用 books.xml 文件作为输入。

<?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>  

注解

XPathNavigator如果当前未定位在元素上,此方法将false返回并更改该元素的位置XPathNavigator

成功调用MoveToFirstAttribute后,属性LocalNameNamespaceURIPrefix属性将反映特性的值。 当定位XPathNavigator在属性上时,方法MoveToNextMoveToPrevious以及MoveToFirst不适用。 这些方法始终返回 false ,不会更改该 XPathNavigator方法的位置。 相反,可以调用 MoveToNextAttribute 移动到下一个属性节点。

定位 XPathNavigator 在属性上后,可以调用 MoveToParent 移动到 owner 元素。

适用于

另请参阅