Problem calling Document.SelectedNodes

SelectedNodes is pulling some of the merge fields truncated. We are using Aspose.Word 10.5.0.0. I wrote a test program:

void Main(string[] args)
{
    Document d = new Document(@"C:\Users\qchy\Desktop\CLOSE_LTR.docx");
    NodeList nList = d.SelectNodes("//FieldStart");
    foreach(Node field in nList)
    Console.WriteLine(field.NextSibling.Range.Text);
    Console.WriteLine();
    // // another approach
    NodeCollection collect = d.GetChildNodes(NodeType.FieldStart, true);
    foreach(FieldStart start in collect)
    Console.WriteLine(start.NextSibling.Range.Text);
    Console.ReadLine();
}

The resulting output shows that some of the fields are truncated:

MERGEFIELD TableStart:CRMTask \* MERGEFORMAT
MERGEFIELD TaskCategoryCodeMergableField \* MERGEFORMAT
MERGEFIELD TaskTypeCodeMergableField \* MERGEFORMAT
MERGEFIELD TaskIdMergableField \* MERGEFORMAT
MERGEFIELD CRMTaskSubjectMergableField \* MERGEFORMAT
MERGEFIELD CRMTaskIdBarcodeMergableField \* MERGEFORMAT
MERGEFIELD CRMTaskResultCodeMergableField \* MERGEFORMAT
MERGE
MERGEFIELD TableStart:IMergableAsset \* MERGEFORMAT
MERGEFIELD LatitudeMergableField \* MERGEFORMAT
MERGEFIELD GISXMergableField \* MERG
MERGEFIELD GISYMergableField \* MERGEFORMAT
MERGEFIELD GISZMergableField \* MERGEFORMAT
MERGEFIELD LocationIdMergableField \* MERGEFORMAT
MERGEFIELD A
MERGEFIELD GISReferenceIDMergableField \* MERGEFORMAT
MERGEFIELD GISSpatialReferenceIDMergableField \* MERGEFORMAT
MERGEFIELD TableEnd:IMergableAsset \* MERGEFORMAT

MERGEFIELD TableStart:CRMTask \* MERGEFORMAT
MERGEFIELD TaskCategoryCodeMergableField \* MERGEFORMAT
MERGEFIELD TaskTypeCodeMergableField \* MERGEFORMAT
MERGEFIELD TaskIdMergableField \* MERGEFORMAT
MERGEFIELD CRMTaskSubjectMergableField \* MERGEFORMAT
MERGEFIELD CRMTaskIdBarcodeMergableField \* MERGEFORMAT
MERGEFIELD CRMTaskResultCodeMergableField \* MERGEFORMAT
MERGE
MERGEFIELD TableStart:IMergableAsset \* MERGEFORMAT
MERGEFIELD LatitudeMergableField \* MERGEFORMAT
MERGEFIELD GISXMergableField \* MERG
MERGEFIELD GISYMergableField \* MERGEFORMAT
MERGEFIELD GISZMergableField \* MERGEFORMAT
MERGEFIELD LocationIdMergableField \* MERGEFORMAT
MERGEFIELD A
MERGEFIELD GISReferenceIDMergableField \* MERGEFORMAT
MERGEFIELD GISSpatialReferenceIDMergableField \* MERGEFORMAT
MERGEFIELD TableEnd:IMergableAsset \* MERGEFORMAT

I attached the file with the issue.
Thanks in advance,
Chi Ho Yeung

Hi there,

Thanks for your inquiry.

This is happening because the text in the field code is split into multiple runs. In other words, you can have full text in a field code which is made up of separate runs.

It seems you are trying to retrieve to the names of merge fields in a document, if so using MailMerge.GetFieldNames will provide the correct results. Please see the following page for details: https://docs.aspose.com/words/net/advanced-mail-merge-features/

If you are looking for the field code of each field in the document then you can use the following code to retrieve the full text even when it is made up of multiple runs:

private static string GetFieldCode(FieldStart fieldStart)
{
    StringBuilder builder = new StringBuilder();

    for (Node node = fieldStart; node != null && node.NodeType != NodeType.FieldSeparator && node.NodeType != NodeType.FieldEnd; node = node.NextPreOrder(node.Document))
    {
        // Use text only of Run nodes to avoid duplication.
        if (node.NodeType == NodeType.Run)
            builder.Append(node.GetText());
    }

    return builder.ToString();
}

If we can help with anything else, please feel free to ask.

Thanks,

That fixed it, thanks!