Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:46:33+00:00 2026-06-10T21:46:33+00:00

this is my sample xml. <Messages> <Conversation> <id>Z100</id> <ReplyToId></ReplyToId> <Message>Topic</Message> </Conversation> <Conversation> <id>A100</id> <ReplyToId></ReplyToId>

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:46:34+00:00Added an answer on June 10, 2026 at 9:46 pm

    Edit: because of your clarification, I have re-written my answer.

    Constructing the Conversation Tree

    1. Use XElement to load the root element and its children from your xml file:

      var root = XElement.Load(@"path\to\your.xml"); 
      // Alternatively, XElement.Parse(stringContainingXML);
      
    2. Transform the elements into conversations (the conversation class is given below):

      var conversations = (from element in root.Elements()
                           select Conversation.FromXElement(element)).ToList();
      // ToList prevents multiple iteration of the IEnumerable
      
    3. Add the replies to each conversation:

      foreach (var conversation in conversations)
      {
          conversation.Replies.AddRange( 
          conversations.Where(reply => reply.ReplyToId == conversation.Id));
      }
      
    4. Optionally, remove the replies from the top level:

      conversations.RemoveAll(x => !string.IsNullOrEmpty(x.ReplyToId));
      
    5. Optionally, to retrieve a single conversation by id:

      Conversation singleConversation = conversations.Single(x => x.Id == "A100");
      

    Result

    Z100: Topic
    A100: This is a test
    *->M100: What kind of test
       *->A200: Stage 1
          *->M200: Test result for
    

    Conversation Helper Class

    public class Conversation
    {
        public string Id { get; private set; }
        public string ReplyToId { get; private set; }
        public string Message { get; private set; }
        public readonly List<Conversation> Replies = new List<Conversation>();
    
        public static Conversation FromXElement(XElement source)
        {
            return new Conversation
            {
                Id = (string)source.Element("id"),
                ReplyToId = (string)source.Element("ReplyToId"),
                Message = (string)source.Element("Message")
            };
        }     
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my sample file: <?xml version=1.0 encoding=UTF-8 ?> <testjar> <testable> <trigger>Trigger1</trigger> <message->2012-06-14T00:03.54</message> <sales-info>
This is the sample xml document : <bookstore> <book category=COOKING> <title lang=english>Everyday Italian</title> <author>Giada
This question's solution makes hard-coded transformation of a sample XML tree into a flat
using LINQ to XML, this is a sample of my XML <shows> <Show Code=456
I have an XML document with some sample content like this: <someTag> <![CDATA[Hello World]]>
In sample soap XML request message, I noticed that there are soap envelope tag
I'm attempting to test a [MessageContract] class against an existing sample message, and I'm
This is a sample of the XML file : <Nodes version=1> <Node name=root> <Node
I have the following XML <ns0:Root xmlns:ns0=http://Map_Sample.Input_Schema> <Number1> </Number1> <Number2>11</Number2> <Number3>12</Number3> </ns0:Root> In this
Consider this simple XML document. The serialized XML shown here is the result of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.