We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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:


<span style=“font-size:8.0pt;font-family:“Lucida Console”;mso-bidi-font-family:“Times New Roman”;
color:blue;mso-no-proof:yes”>static<span style=“font-size:8.0pt;
font-family:“Lucida Console”;mso-bidi-font-family:“Times New Roman”;mso-no-proof:
yes”> void Main(string[]
args)<o:p></o:p>

{

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: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-use-advanced-mail-merge-features.html

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)<o:p></o:p>

{<o:p></o:p>

StringBuilder builder = new StringBuilder();<o:p></o:p>

<o:p> </o:p>

for (Node node = fieldStart; node != null && node.NodeType != NodeType.FieldSeparator && node.NodeType != NodeType.FieldEnd; node = node.NextPreOrder(node.Document))<o:p></o:p>

{<o:p></o:p>

// Use text only of Run nodes to avoid duplication.<o:p></o:p>

if (node.NodeType == NodeType.Run)<o:p></o:p>

builder.Append(node.GetText());<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

return builder.ToString();<o:p></o:p>

}


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

Thanks,

That fixed it, thanks!