XPathNavigator.MoveToFirstAttribute 方法

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

**命名空间:**System.Xml.XPath
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
Public MustOverride Function MoveToFirstAttribute As Boolean
用法
Dim instance As XPathNavigator
Dim returnValue As Boolean

returnValue = instance.MoveToFirstAttribute
public abstract bool MoveToFirstAttribute ()
public:
virtual bool MoveToFirstAttribute () abstract
public abstract boolean MoveToFirstAttribute ()
public abstract function MoveToFirstAttribute () : boolean

返回值

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

备注

如果 XPathNavigator 当前未定位在元素上,则此方法将返回 falseXPathNavigator 的位置不变。

成功调用 MoveToFirstAttribute 后,LocalNameNamespaceURIPrefix 属性反映该属性的值。当 XPathNavigator 定位在属性上时,方法 MoveToNextMoveToPreviousMoveToFirst 不适用。这些方法总是返回 false,并且不更改 XPathNavigator 的位置。相反,可以调用 MoveToNextAttribute 移动到下一个属性节点。

XPathNavigator 定位在属性上之后,可以通过调用 MoveToParent 来移动到所有者元素。

示例

下面的示例使用 MoveToFirstAttributeMoveToNextAttribute 方法来显示 books.xml 文件中每本书的所有属性。

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
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();
}

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

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

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

XPathNavigator 类
XPathNavigator 成员
System.Xml.XPath 命名空间
MoveToAttribute
MoveToNextAttribute
GetAttribute
HasAttributes