XPathNavigator.SelectAncestors Метод

Определение

Выбирает все узлы-предки текущего узла, соответствующие условиям выбора.

Перегрузки

SelectAncestors(XPathNodeType, Boolean)

Выбирает все узлы-предки текущего узла с совпадающим XPathNodeType.

SelectAncestors(String, String, Boolean)

Выбирает все узлы-предки текущего узла с заданным локальным именем и URI пространства имен.

SelectAncestors(XPathNodeType, Boolean)

Исходный код:
XPathNavigator.cs
Исходный код:
XPathNavigator.cs
Исходный код:
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)

Исходный код:
XPathNavigator.cs
Исходный код:
XPathNavigator.cs
Исходный код:
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.

См. также раздел

Применяется к