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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:13:03+00:00 2026-06-07T10:13:03+00:00

I have an XML document like this <root> <item id=1 creator=me> <childA>1</childA> <childB>2</childB> </item>

  • 0

I have an XML document like this

<root>
  <item id="1" creator="me">
    <childA>1</childA>
    <childB>2</childB>
  </item>
  <item id="2" creator="me">
    <childA>1</childA>
    <childB>3</childB>
    <childB>4</childB>
  </item>
</root>

I’m trying to find duplicate items, then again duplicate child items for the duplicate items with logic like this

XDocument XmlRoot //whatever...you get the point

// Get item nodes
var items = XmlRoot.Descendants("item");

// Find duplicate items keys using creator attribute
var duplicateItemKeys = items.GroupBy(x => x.Attribute("creator").Value)
.Where(g => g.Count() > 1)
.Select(g => g.Key);

foreach(var duplicateItemKey in duplicateItemKeys)
{
  // Get the duplicate item XML elements using the duplicate keys
  var duplicateItems = items.Where(x => x.Attribute("creator").Value == duplicateToucheKey)
      .OrderBy(xelement => xelement.Attribute("CreatedOn").Value);
}

This works, however there is a problem later when I try to use duplicateItems. Any time it enumerates (like in a foreach duplicateItems) the first item looses the context of it’s children. The second one is just fine.

So for example, later in code I say

var allItemB = new List<XElement>();
foreach (duplicateItem in duplicateItems) 
{
  allItemB.AddRange(duplicateItem.Descendants("childB"));
}

I expect “allItemB” to contain 2 on the first pass, then 234 on the second. What ends up happening is that it only contains 34 because once the duplicateItems array is enumerated the first XElement looses it’s children.

Does anyone know how to fix this?

  • 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-07T10:13:05+00:00Added an answer on June 7, 2026 at 10:13 am

    If I understand your question correctly, you want allItemB to have 3 elements – allItemB[0] is the XElement childB with value 2, [1] to be 3 and [2] to be 4?

    If so, the problem is where you are declaring duplicateItems. Your code same doesn’t even compile, as the variable’s scope is limited to the first foreach loop, and thus not available at the second.

    My code to get the above result:

    XDocument XmlRoot = XDocument.Load( "C:\\somefile.xml" );
    
    // Get item nodes
    var items = XmlRoot.Descendants("item");
    
    // Find duplicate items keys using creator attribute
    var duplicateItemKeys = items.GroupBy(x => x.Attribute("creator").Value)
         .Where(g => g.Count() > 1)
         .Select(g => g.Key);
    
    IEnumerable<XElement> duplicateItems = new List<XElement>();
    foreach(var duplicateItemKey in duplicateItemKeys)
    {
         // Get the duplicate item XML elements using the duplicate keys
         duplicateItems = items.Where(x => x.Attribute("creator").Value == duplicateItemKey)
              .OrderBy(xelement => xelement.Attribute("id").Value);
     }
    
     var allItemB = new List<XElement>();
     foreach (var duplicateItem in duplicateItems) 
     {
          allItemB.AddRange(duplicateItem.Descendants("childB"));
     }
    

    Edit: forgot to mention that I changed the OrderBy in the first foreach loop because the sample xml file didn’t have the CreatedOn attribute.

    And if you want, you can use a little more Linq and drop the foreach loops entirely, like so:

    XDocument XmlRoot = XDocument.Load( "C:\\somefile.xml" );
    
    // Get item nodes
    var items = XmlRoot.Descendants("item");
    
    // Find duplicate items keys using creator attribute
    var duplicateItemKeys = items.GroupBy(x => x.Attribute("creator").Value)
         .Where(g => g.Count() > 1)
         .Select(g => g.Key);
    
    // Get the duplicate item XML elements using the duplicate keys
    var duplicateItems = items.Where(i => duplicateItemKeys.Contains(i.Attribute("creator").Value))
         .OrderBy( xelement => xelement.Attribute("id").Value );
    
    // Get the child nodes named childB
    var allItemB = new List<XElement>();
    allItemB.AddRange( duplicateItems.Descendants("childB") );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I have an XML document that looks something like this: <root> <profile> <childA>
Hi I have an xml document like this <root> <cert id=1> </cert> <cert id=2>
I have got an XML document which looks something like this. <Root> <Info>....</Info> <Info>....</Info>
I have an XML structure that looks like this: <root> <index> <item>item 1</item> <item>item
I have an xml document like this: <?xml version=1.0 encoding=UTF-8?> <foo:root xmlns:foo=http://abc.com# xmlns:bar=http://def.com xmlns:ex=http://ex.com>
I have an XML document, something like <root> <item>_x0034_SOME TEXT</item> <item>SOME_x0020_TEXT</item> <item>SOME_x0020_TEXT_x0032_</item> </root> I'm
If I have an XML document like this: <root> <elem name=Greeting> Hello </elem> <elem
I have an Xml like this: <root> <item> <content type=HTML> <![CDATA[<html> <head></head> <body>something goes
I have a xml document like this : <Node1 attrib1=abc> <node1_1> <node1_1_1 attrib2 =
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley

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.