public void TestNodeIsInsideField()
{
//ExStart:
//ExFor:CompositeNode.SelectNodes
//ExFor:CompositeNode.GetChild
//ExSummary: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 = doc.SelectNodes("//FieldStart/following-sibling::node()[following-sibling::FieldEnd]");
// 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");
break;
}
}
//ExEnd
}