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 8320873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:49:00+00:00 2026-06-08T22:49:00+00:00

I am trying to figure out how to group XML nodes with the same

  • 0

I am trying to figure out how to group XML nodes with the same name but different values.

The web service I’m using returns an XmlElement that looks like this:

<Items>
    <Item>
        <Name name="Name">Item 1</Name>
        <Description name="Description">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </Description>
        <AssociatedItems name="Associated Items">Item 2</AssociatedItems>
        <AssociatedItems name="Associated Items">Item 3</AssociatedItems>
        <AssociatedItems name="Associated Items">Item 4</AssociatedItems>
        <AssociatedItems name="Associated Items">Item 5</AssociatedItems>
    </Item>
</Items>

I am transforming each node into an HTML tag

protected void lnkItem_Click(object sender, EventArgs e)
{
    LinkButton link = (LinkButton)sender;
    GridViewRow row = (GridViewRow)link.Parent.Parent;

    string id = gv.DataKeys[row.RowIndex]["id"].ToString();

    PublicApiAsmxServiceSoapClient service = new PublicApiAsmxServiceSoapClient("PublicApiAsmxServiceSoap", WEB_SERVICE_URL);
    XmlElement xml = service.ItemGetAsXml(id);
    XElement nodes = XElement.Parse(xml.InnerXml);

    foreach (var node in nodes.Elements())
    {
        InsertHTML(node);
    }
}

private void InsertHTML(XElement node)
{
    if (node.Value == string.Empty)
        return;

    pnl.ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<h3>{0}</h3>", node.Attribute("name").Value)));
    pnl.ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<p>{0}</p>", node.Value)));
}

With my code right now, the HTML output would be:

<h3>Name</h3>
<p>Item 1</p>
<h3>Description</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<h3>Associated Items</h3>
<p>Item 2</p>
<h3>Associated Items</h3>
<p>Item 3</p>
<h3>Associated Items</h3>
<p>Item 4</p>
<h3>Associated Items</h3>
<p>Item 5</p>

Is there a way to group the same nodes as an unordered list? Something like this, perhaps:

<h3>Name</h3>
<p>Item 1</p>
<h3>Description</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<h3>Associated Items</h3>
<ul>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
</ul>

Thank you! (Please be nice.)

PS
The child nodes that repeat aren’t limited to AssociatedTerms only. There are possibly more child nodes of the same name that repeat.

  • 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-08T22:49:01+00:00Added an answer on June 8, 2026 at 10:49 pm

    It would be easiest using a generator to generate the elements based on the groupings since what gets generated depends on how many duplicates there are.

    As I understand it, you’re grouping by element name and the value of the name attributes. Then generating an h3 followed by an element depending on if there are multiple elements in the group. p if there’s a single element or a ul if there are multiple elements.

    IEnumerable<XElement> GroupedElements(XElement root)
    {
        var groupedItems =
            from element in root.Elements()
            group element
            by new
            {
                Element = element.Name,
                Name = (string)element.Attribute("name"),
            };
        foreach (var g in groupedItems)
        {
            yield return new XElement("h3", g.Key.Name);
            var isMultiple = g.Skip(1).Any();
            if (isMultiple)
                yield return new XElement("ul",
                    from item in g
                    select new XElement("li", item.Value.Trim())
                );
            else
                yield return new XElement("p", g.Single().Value.Trim());
        }
    }
    
    var xmlStr = @"<Items>
        <Item>
            <Name name=""Name"">Item 1</Name>
            <Description name=""Description"">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            </Description>
            <AssociatedItems name=""Associated Items"">Item 2</AssociatedItems>
            <AssociatedItems name=""Associated Items"">Item 3</AssociatedItems>
            <AssociatedItems name=""Associated Items"">Item 4</AssociatedItems>
            <AssociatedItems name=""Associated Items"">Item 5</AssociatedItems>
        </Item>
    </Items>";
    var doc = XDocument.Parse(xmlStr);
    var transformed = new XElement("div", 
        from item in doc.XPathSelectElements("/Items/Item")
        select GroupedElements(item)
    );
    

    Yields the following:

    <div>
      <h3>Name</h3>
      <p>Item 1</p>
      <h3>Description</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <h3>Associated Items</h3>
      <ul>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
      </ul>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out why my web app (I didn't write it, but
I'm trying to figure out a way to return results by using the group
I am trying to figure out how to group data contained inside a Table.
Im trying to figure out how to do: group has_many participants participant belongs_to group
for the last hour I have been trying to figure this out myself but
I am trying to figure out how I would setup a group of images
XML confuses me sometimes, but I'm trying to get this figured out. What the
I am trying to figure out a way to create a class to group
I'm trying to figure out the best way to do this...I am using JQuery
I'm trying to figure out how to GROUP BY on multiple columns. I want

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.