CompositeNodeSelectNodes Method
Selects a list of nodes matching the XPath expression.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 20.1
public NodeList SelectNodes(
	string xpath


Type: SystemString
The XPath expression.

Return Value

Type: NodeList
A list of nodes matching the XPath query.

Only expressions with element names are supported at the moment. Expressions that use attribute names are not supported.

Shows how to test if a node is inside a field by using an XPath expression.
// Let's pick a document we know has some fields in
Document doc = new Document(MyDir + "MailMerge.MergeImage.doc");

// Let's say we want to check if the Run below is inside a field
Run run = (Run) doc.GetChild(NodeType.Run, 5, true);

// Evaluate the XPath expression. The resulting NodeList will contain all nodes found inside a field a field (between FieldStart 
// and FieldEnd exclusive). There can however be FieldStart and FieldEnd nodes in the list if there are nested fields 
// in the path. Currently does not find rare fields in which the FieldCode or FieldResult spans across multiple paragraphs
NodeList resultList =

// Check if the specified run is one of the nodes that are inside the field
foreach (Node node in resultList)
    if (node == run)
        Console.WriteLine("The node is found inside a field");
Shows how to select certain nodes by using an XPath expression.
Document doc = new Document(MyDir + "Table.Document.doc");

// This expression will extract all paragraph nodes which are descendants of any table node in the document
// This will return any paragraphs which are in a table
NodeList nodeList = doc.SelectNodes("//Table//Paragraph");

// Iterate through the list with an enumerator and print the contents of every paragraph in each cell of the table
int index = 0;
using (IEnumerator<Node> e = nodeList.GetEnumerator())
    while (e.MoveNext())
        Console.WriteLine($"Table paragraph index {index++}, contents: \"{e.Current.GetText().Trim()}\"");

// This expression will select any paragraphs that are direct children of any body node in the document
nodeList = doc.SelectNodes("//Body/Paragraph");

// We can treat the list as an array too
Assert.AreEqual(4, nodeList.ToArray().Length);

// Use SelectSingleNode to select the first result of the same expression as above
Node node = doc.SelectSingleNode("//Body/Paragraph");
See Also