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

  • SEARCH
  • Home
  • 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 7874101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:42:49+00:00 2026-06-03T02:42:49+00:00

I am trying to add a sub category to Messages in my xml statement

  • 0

I am trying to add a sub category to Messages in my xml statement Is there a way I can do this GroupMessages -> Message -> GroupMessage :

        var groups = xDoc.Descendants("Group")
            .Select(n => new
            {
                GroupName = n.Element("GroupName").Value,
                GroupHeader = n.Element("GroupHeader").Value,
                TimeCreated = DateTime.Parse(n.Element("TimeAdded").Value),
                Tags = n.Element("Tags").Value, 
                Messages = n.Element("GroupMessages").Value
                //line above
            })
            .ToList();
        dataGrid2.ItemsSource = groups;

In my method GroupMessages contains both MessageID and GroupMessage and it is listing both in my datagrid within the one container. So I tried this but it lists nothing:

 Messages = n.Descendants("GroupMessages").Select(nd => nd.Element("GroupMessage").Value)

My XML looks like this:

<Group>
<TimeAdded>2012-04-27T10:23:50.7153613+01:00</TimeAdded>
<GroupName>Group</GroupName>
<GroupHeader>Header</GroupHeader>
<GroupMessages>
<Message>
<MessageID>1</MessageID>
<GroupMessage>Message</GroupMessage>
<MessageGroup/>
</Message>
</GroupMessages>
</Group>

I have also tried:

Messages = n.Descendants("GroupMessages").Select(nd => nd.Descendants("Message").Select(nde => nde.Element("GroupMessage").Value))

To no avail?

Update:

    private void ListGroups_Click(object sender, RoutedEventArgs e)
    {
        string uriGroup = "http://localhost:8000/Service/Group";
        XDocument xDoc = XDocument.Load(uriGroup);
        var groups = xDoc.Descendants("Group")
                    .Select(n => new
        {
            GroupName = n.Element("GroupName").Value,
            GroupHeader = n.Element("GroupHeader").Value,
            TimeCreated = n.Element("TimeAdded").Value,
            Tags = n.Element("Tags").Value,
            Messages = n.Element("GroupMessages").Descendants("Message").Select(nd => new
            {
                //Id = nd.Element("MessageID").Value,
                Message = nd.Element("GroupMessage").Value
            }).FirstOrDefault()
        })
        .ToList();
        dataGrid2.ItemsSource = groups;
    }

Unfortunatley this method shows “Collection” inside the cell in the datagrid. If I try ToArray it will show an array message inside the cell. Is there a way to actually display the GroupMessage? Not sure how you set the child elements of a datagrid?

  • 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-03T02:42:52+00:00Added an answer on June 3, 2026 at 2:42 am

    At the most basic level, you can do this to get a single message (the first one):

    var groups = from grp in xDoc.Descendants("Group")
                 select new { 
                    GroupName = grp.Element("GroupName").Value,
                    GroupHeader = grp.Element("GroupHeader").Value,
                    TimeCreated = DateTime.Parse(grp.Element("TimeAdded").Value),
                    Message = grp.Element("GroupMessages").Element("Message").Element("GroupMessage").Value
                 };
    

    However, I assume that you want Messages to be a list of messages with both ID and Message. In that case, consider this:

    var groups = from grp in xDoc.Descendants("Group")
                 select new { 
                    GroupName = grp.Element("GroupName").Value,
                    GroupHeader = grp.Element("GroupHeader").Value,
                    TimeCreated = DateTime.Parse(grp.Element("TimeAdded").Value),
                    Messages = grp.Element("GroupMessages")
                                 .Descendants("Message")
                                 .Select(msg => new { 
                                     Id = msg.Element("MessageID").Value, 
                                     Message = msg.Element("GroupMessage").Value
                                 }).ToList()
                 };
    

    However, I strongly stress that all this usage of anonymous classes is just going to cause confusion. If you have a class for Group and Message then use those.

    Note that the problem you’re having is you’re ignoring the XML structure and selecting random elements. To get the value out of a single element, you’re going to need to select exactly that element, and ask for .Value. Selecting it’s parent, or it’s parent’s parent (as you did) is not enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to add email notification to my app in the cleanest way possible. When
Im trying to add a directory foo to my repo, but there are some
Im trying to add a constraint to a reservation table, the reservation can either
I am trying to add a sub view to the main view when a
I'm trying to add elements to a sub array of the sub array where
This is the script I'm trying to add a last argument to that prints
Trying to add a 'box' to a form at design time, I looked up
Trying to add tests to the gobject-introspection test suite to cover more of the
Trying to add a class object into a List using reflection, but when invoking
Trying to add a dynamic class value to one of my columns in CGridView:

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.