this is my sample xml.
<Messages>
<Conversation>
<id>Z100</id>
<ReplyToId></ReplyToId>
<Message>Topic</Message>
</Conversation>
<Conversation>
<id>A100</id>
<ReplyToId></ReplyToId>
<Message>This is a test</Message>
</Conversation>
<Conversation>
<id>M100</id>
<ReplyToId>A100</ReplyToId>
<Message>What kind of test</Message>
</Conversation>
<Conversation>
<id>A200</id>
<ReplyToId>M100</ReplyToId>
<Message>Stage 1</Message>
</Conversation>
<Conversation>
<id>M200</id>
<ReplyToId>A200</ReplyToId>
<Message>Test result for </Message>
</Conversation>
</Messages>
How to get the conversation list based on id using linq in C#. Say for example, If i want to get the conversation for id “A100” which has a link to other conversation based on ReplyToid.
Edit: because of your clarification, I have re-written my answer.
Constructing the Conversation Tree
Use
XElementto load the root element and its children from your xml file:Transform the elements into conversations (the conversation class is given below):
Add the replies to each conversation:
Optionally, remove the replies from the top level:
Optionally, to retrieve a single conversation by id:
Result
Z100: Topic A100: This is a test *->M100: What kind of test *->A200: Stage 1 *->M200: Test result forConversation Helper Class