GetChildNodes - within certain sections or form fields only

I am using the code below to replace hyphens with nonbreaking hyphens to address some business rules required formatting.

// Replace hyphens with non breaking hyphens
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
foreach (Run run in runs)
    // only replace for endorsementids (which contain two spaces together and hyphens)
    if (run.Text.Contains("-") && run.Text.Contains(" "))
        run.Text = run.Text.Replace("-", "" + ControlChar.NonBreakingHyphenChar);

The code works, but it iterates through the entire document. I know the only places that need to have the replacement done are always in the first two sections of the document. Also, the text is always within a form field.

Is there away to limit the code to run only with certain sections or only on text with a form field?

Hi Doug,

Thanks for your inquiry. In your case, we suggest you please use find and replace feature of Aspose.Words. Please check following code snippet for your kind reference. If you still face problem, please share your input document here for testing.

Document doc = new Document(MyDir + "in.docx");
doc.Range.Replace("-", ControlChar.NonBreakingSpace, false, false);