Wrong index coming

I have list paragraphs which i got with the help of Getting Child from current story and fetching only the Paragraph nodes

var para = doc.CurrentStory.GetChildNodes(NodeType.Paragraph, true)[para.ParagraphIndex];

and then im fetching the index using this

index = para.Range.Text.IndexOf(index);

but its giving me wrong index because my para’s result also have comment nodes in it
can you tell me a way how can i fetch correct index ignoring the comment nodes

@Amrinder_Singh Node.Range.Text returns text content of all child nodes of the node. Since Comment is child node of the paragraph it’s text is also included. You can remove comments from the paragraph before getting the text:

Paragraph paraWithoutComments = (Paragraph)para.Clone(true);
paraWithoutComments.GetChildNodes(NodeType.Comment, true).Clear();
string paraTextWithoutComments = paraWithoutComments.ToString(SaveFormat.Text);

Also, Node.Range.Text includes special characters and field code in the output, so I would suggest you to use ToString method instead as shown in the code example above.

Thank you @alexey.noskov
It worked

1 Like