When two or more RichText StructuredDocumentTag nodes (SDTs) are nested inside of each other, the number of items returned by ‘GetChildNodes’ is inconsistent.
As an example, I have created the following docx file:
SdtParasTest.docx (20.2 KB)
It has two SDTs: one inside the other, with each containing 2 direct child paragraphs. There is an extra paragraph before the SDTs, and 5 extra paragraphs after.
Performing a deep search for child paragraph nodes appears to return correct results (4 children for the parent SDT, 2 children for the child SDT). However, performing a shallow search is a different story.
For the parent SDT, a shallow paragraph search returns 4 paragraphs as it is incorrectly including the paragraphs of the child SDT. And for the child SDT, a shallow paragraph search returns 7 paragraphs as it is incorrectly including all of the paragraphs that follow the SDTs.
Here is the code I am using:
Document doc = new("SdtParasTest.docx");
// Check the child paragraphs of the parent sdt
IStructuredDocumentTag sdt1 = doc.Range.StructuredDocumentTags[0];
NodeCollection shallowParagraphs1 = sdt1.GetChildNodes(NodeType.Paragraph, false);
Assert.AreEqual(2, shallowParagraphs1.Count); // Fails: returns 4
NodeCollection deepParagraphs1 = sdt1.GetChildNodes(NodeType.Paragraph, true);
Assert.AreEqual(4, deepParagraphs1.Count);
// Check the child paragraphs of the child sdt
IStructuredDocumentTag sdt2 = doc.Range.StructuredDocumentTags[1];
NodeCollection shallowParagraphs2 = sdt2.GetChildNodes(NodeType.Paragraph, false);
Assert.AreEqual(2, shallowParagraphs2.Count); // Fails: returns 7
NodeCollection deepParagraphs2 = sdt2.GetChildNodes(NodeType.Paragraph, true);
Assert.AreEqual(2, deepParagraphs2.Count);