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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:12:54+00:00 2026-06-04T13:12:54+00:00

I need to group Document value from XML. Problem is because key value (productType)

  • 0

I need to group “Document” value from XML. Problem is because key value (productType) can be multiple.

This is XML:

<Documents>
<Document>
    <id>1</id>
    <title>title1</title>
    <productTypes>
        <productType id="x1">Capital Costs Analysis Forum - Brazil</productType>
  <productType id="x3">Environmental, Health and Safety &amp; Sustainability</productType>
    </productTypes>
</Document>
<Document>
    <id>2</id>
    <title>title2</title>
    <productTypes>
  <productType id="x1">Capital Costs Analysis Forum - Brazil</productType>
    </productTypes>
</Document>
<Document>
    <id>3</id>
    <title>title3</title>
    <productTypes>
  <productType id="x3">Environmental, Health and Safety &amp; Sustainability</productType>
    </productTypes>
</Document>
<Document>
    <id>4</id>
    <title>title4</title>
    <productTypes>
        <productType id="x2">Defense, Risk &amp; Security</productType>
    </productTypes>
</Document>

And this is what I try:

var documents = from document in some.Descendants("Document")
                group document by (string)document
                    .Element("productTypes")
                    .Elements("productType")
                    .First() into docGroup
select docGroup;

My code is working only if is there one productType element. How to change my code to work if there is multiple value of productType?

  • 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-04T13:12:55+00:00Added an answer on June 4, 2026 at 1:12 pm

    You did not explain what result you want but I suspect you want the following grouping:

            var documentGroups = 
                from document in XDocument.Load("input.xml").Descendants("Document")
                from productType in document.Element("productTypes").Elements("productType")
                group document by (string)productType.Attribute("id");
    
            foreach (var documentGroup in documentGroups)
            {
                Console.WriteLine("Group {0} has the following members:", documentGroup.Key);
                foreach (XElement document in documentGroup)
                {
                    Console.WriteLine("\t{0}", (string)document.Element("title"));
                }
                Console.WriteLine();
            }
    

    With the input being

    <Documents>
      <Document>
        <id>1</id>
        <title>title1</title>
        <productTypes>
          <productType id="x1">Capital Costs Analysis Forum - Brazil</productType>
          <productType id="x3">Environmental, Health and Safety &amp; Sustainability</productType>
        </productTypes>
      </Document>
      <Document>
        <id>2</id>
        <title>title2</title>
        <productTypes>
          <productType id="x1">Capital Costs Analysis Forum - Brazil</productType>
        </productTypes>
      </Document>
      <Document>
        <id>3</id>
        <title>title3</title>
        <productTypes>
          <productType id="x3">Environmental, Health and Safety &amp; Sustainability</productType>
        </productTypes>
      </Document>
      <Document>
        <id>4</id>
        <title>title4</title>
        <productTypes>
          <productType id="x2">Defense, Risk &amp; Security</productType>
        </productTypes>
      </Document>
    </Documents>
    

    that outputs

    Group x1 has the following members:
            title1
            title2
    
    Group x3 has the following members:
            title1
            title3
    
    Group x2 has the following members:
            title4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the XML below, I need to group by InputItem and sum the costs
My business problem is to group emails by sender, where sender we need to
This question is geared towards a group of newly hired developers that need to
I am trying to parse some XML that looks similar to this: <document> <headings>
I need to group the following XML doc to show: Parent Item Qty ----------------------------------
Problem: I have a 'Group' collection. Each group has an embedded document of 'Members'.
I have an XML document that i need to strip out particular pieces of
Hi All I have a document with this structure. <Employee> <Group Id=1> <Employee Id=2
I need to parse a xml document with Java and output another xml document.
$(document).ready(function () { $('.group').hide(); $('#selectMe').change(function () { $('.group').hide(); $('#' +$(this).val()).show(); }) }); <html> <label

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.