How to get comments by level?

Hi:
I want to get comments by level . Test.docx (26.6 KB)
The first comments level collection is ‘add comment 1’ and ‘add comment 2’ and ‘add comment 3’.
I want to get the collection.How can I do? Can you give me a example?

source code like this:

NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);

get all the comments collection. I can’t distinguish which level the comment belongs to.

@JohnKj You can use Comment.Replies to access the comment’s replies (child comments). Please see our documentation to learn more:
https://docs.aspose.com/words/net/working-with-comments/#read-comments-reply

I don’t think they can fit my needs.I just know how to get all the comments or get the comments by author,but I can’t get comments where are fist applies.I‘m not familiar with other operations. Can you give me a example by the word where I upload?

@JohnKj You can try using the following code to get comments hierarchy:

Document doc = new Document(@"C:\Temp\in.docx");

// Gte all comments
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Collect reply comments.
List<Comment> replyComments = new List<Comment>();
foreach (Comment c in comments)
    replyComments = replyComments.Union(c.Replies.Cast<Comment>()).ToList();

// Get list of top level comments.
List<Comment> topComments = doc.GetChildNodes(NodeType.Comment, true).Cast<Comment>()
.Where(c => !replyComments.Contains(c)).ToList();

// Print comments hierarchy
foreach (Comment c in topComments)
{
    Console.WriteLine($"{c.Author}: {c.ToString(SaveFormat.Text).Trim()}");
    foreach (Comment reply in c.Replies)
    {
        Console.WriteLine($"\t{reply.Author}: {reply.ToString(SaveFormat.Text).Trim()}");
    }
}

For your document the output is the following:

Group9 John: add comment 1
        Group9 MM: replay comment 2
        Group9 Lucy: inner comment 3
Group9 John: add comment 2
        Group9 MM: replay comment 2
Group9 Lucy: add comment 3

Thanks for your help.

1 Like