XPathNavigator.SelectAncestors 方法

定义

选择当前节点中与选择条件匹配的所有上级节点。

重载

SelectAncestors(XPathNodeType, Boolean)

选择当前节点中具有匹配的 XPathNodeType 的所有上级节点。

SelectAncestors(String, String, Boolean)

选择当前节点中具有指定的本地名称和命名空间 URI 的所有上级节点。

SelectAncestors(XPathNodeType, Boolean)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

选择当前节点中具有匹配的 XPathNodeType 的所有上级节点。

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ SelectAncestors(System::Xml::XPath::XPathNodeType type, bool matchSelf);
public virtual System.Xml.XPath.XPathNodeIterator SelectAncestors (System.Xml.XPath.XPathNodeType type, bool matchSelf);
abstract member SelectAncestors : System.Xml.XPath.XPathNodeType * bool -> System.Xml.XPath.XPathNodeIterator
override this.SelectAncestors : System.Xml.XPath.XPathNodeType * bool -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function SelectAncestors (type As XPathNodeType, matchSelf As Boolean) As XPathNodeIterator

参数

type
XPathNodeType

上级节点的 XPathNodeType

matchSelf
Boolean

若要在所选内容上包含上下文节点,则为 true;否则为 false

返回

一个包含所选节点的 XPathNodeIterator。 返回的节点的顺序与文档顺序相反。

示例

有关选择上级节点的示例,请参阅 XPathNavigator.SelectAncestors

注解

方法 SelectAncestors 对 的状态 XPathNavigator没有影响。

另请参阅

适用于

SelectAncestors(String, String, Boolean)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

选择当前节点中具有指定的本地名称和命名空间 URI 的所有上级节点。

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ SelectAncestors(System::String ^ name, System::String ^ namespaceURI, bool matchSelf);
public virtual System.Xml.XPath.XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf);
abstract member SelectAncestors : string * string * bool -> System.Xml.XPath.XPathNodeIterator
override this.SelectAncestors : string * string * bool -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function SelectAncestors (name As String, namespaceURI As String, matchSelf As Boolean) As XPathNodeIterator

参数

name
String

祖先节点的本地名称。

namespaceURI
String

上级节点的命名空间 URI。

matchSelf
Boolean

如果要在所选内容中包括上下文节点,则为 true;否则为 false

返回

一个包含所选节点的 XPathNodeIterator。 返回的节点的顺序与文档顺序相反。

例外

无法将 null 作为一个参数进行传递。

示例

以下示例演示了如何选择上级节点、子节点和子节点。

XPathDocument^ document = gcnew XPathDocument("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");

// Select all the descendant nodes of the book node.
XPathNodeIterator^ bookDescendants = navigator->SelectDescendants("", "http://www.contoso.com/books", false);

// Display the LocalName of each descendant node.
Console::WriteLine("Descendant nodes of the book node:");
while (bookDescendants->MoveNext())
{
    Console::WriteLine(bookDescendants->Current->Name);
}

// Select all the child nodes of the book node.
XPathNodeIterator^ bookChildren = navigator->SelectChildren("", "http://www.contoso.com/books");

// Display the LocalName of each child node.
Console::WriteLine("\nChild nodes of the book node:");
while (bookChildren->MoveNext())
{
    Console::WriteLine(bookChildren->Current->Name);
}

// Select all the ancestor nodes of the title node.
navigator->MoveToChild("title", "http://www.contoso.com/books");

XPathNodeIterator^ bookAncestors = navigator->SelectAncestors("", "http://www.contoso.com/books", false);

// Display the LocalName of each ancestor node.
Console::WriteLine("\nAncestor nodes of the title node:");

while (bookAncestors->MoveNext())
{
    Console::WriteLine(bookAncestors->Current->Name);
}
XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");

// Select all the descendant nodes of the book node.
XPathNodeIterator bookDescendants = navigator.SelectDescendants("", "http://www.contoso.com/books", false);

// Display the LocalName of each descendant node.
Console.WriteLine("Descendant nodes of the book node:");
while (bookDescendants.MoveNext())
{
    Console.WriteLine(bookDescendants.Current.Name);
}

// Select all the child nodes of the book node.
XPathNodeIterator bookChildren = navigator.SelectChildren("", "http://www.contoso.com/books");

// Display the LocalName of each child node.
Console.WriteLine("\nChild nodes of the book node:");
while (bookChildren.MoveNext())
{
    Console.WriteLine(bookChildren.Current.Name);
}

// Select all the ancestor nodes of the title node.
navigator.MoveToChild("title", "http://www.contoso.com/books");

XPathNodeIterator bookAncestors = navigator.SelectAncestors("", "http://www.contoso.com/books", false);

// Display the LocalName of each ancestor node.
Console.WriteLine("\nAncestor nodes of the title node:");

while (bookAncestors.MoveNext())
{
    Console.WriteLine(bookAncestors.Current.Name);
}
Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")

' Select all the descendant nodes of the book node.
Dim bookDescendants As XPathNodeIterator = navigator.SelectDescendants("", "http://www.contoso.com/books", False)

' Display the LocalName of each descendant node.
Console.WriteLine("Descendant nodes of the book node:")
While bookDescendants.MoveNext()
    Console.WriteLine(bookDescendants.Current.Name)
End While

' Select all the child nodes of the book node.
Dim bookChildren As XPathNodeIterator = navigator.SelectChildren("", "http://www.contoso.com/books")

' Display the LocalName of each child node.
Console.WriteLine(vbCrLf & "Child nodes of the book node:")
While bookChildren.MoveNext()
    Console.WriteLine(bookChildren.Current.Name)
End While

' Select all the ancestor nodes of the title node.
navigator.MoveToChild("title", "http://www.contoso.com/books")

Dim bookAncestors As XPathNodeIterator = navigator.SelectAncestors("", "http://www.contoso.com/books", False)

' Display the LocalName of each ancestor node.
Console.WriteLine(vbCrLf & "Ancestor nodes of the title node:")

While bookAncestors.MoveNext()
    Console.WriteLine(bookAncestors.Current.Name)
End While

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

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <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>  

注解

如果将 String.Empty 指定为 name 参数,则选择属于指定命名空间 URI 的所有上级节点。 如果将 String.Empty 指定为 namespaceURI 参数,则选择具有指定本地名称且不属于任何命名空间的所有上级节点。 如果 String.Empty 同时指定为本地名称和命名空间 URI,则选择不属于任何命名空间的所有上级节点。

方法 SelectAncestors 对 的状态 XPathNavigator没有影响。

另请参阅

适用于