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。 如果為 falseXPathNavigator 的位置就不會變更。

範例

下列範例會使用 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當 位於屬性上時,方法 MoveToNextMoveToPreviousMoveToFirst 都不適用。 這些方法一律會傳回 false ,而且不會變更 的位置 XPathNavigator 。 相反地,您可以呼叫 MoveToNextAttribute 以移至下一個屬性節點。

XPathNavigator在 位於屬性上之後,您可以呼叫 MoveToParent 以移至擁有者專案。

適用於

另請參閱