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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:19:18+00:00 2026-05-20T11:19:18+00:00

I’m new to LINQ and C# but I have an xml file generated from

  • 0

I’m new to LINQ and C# but I have an xml file generated from a database table. Within the XML document is a element called “group”, I would like to wrap all group elements with the element called “groups”.

An extract of the XML document is:

<members>
- <user>
  <fullname>John Smith</fullname> 
  <username>SmithA</username> 
  <email>John.smith@test.com/email> 
  <distinguishedName>xxx</distinguishedName> 
  <group>London</group>
  </user>
- <user>
  <fullname>Sue Jones</fullname> 
  <username>JonesS</username> 
  <email>Sue.Jones@test.com/email> 
  <distinguishedName>xxx</distinguishedName> 
  <group>London</group>
  </user>
</members>

The end result I struggling to code in C# ASP.NET is:

<members>
- <user>
  <fullname>John Smith</fullname> 
  <username>SmithA</username> 
  <email>John.smith@test.com/email> 
  <distinguishedName>xxx</distinguishedName> 
  <groups>
  <group>London</group>
  <groups>
  </user>
- <user>
  <fullname>Sue Jones</fullname> 
  <username>JonesS</username> 
  <email>Sue.Jones@test.com/email> 
  <distinguishedName>xxx</distinguishedName> 
  <groups>
  <group>London</group>
  <groups>
  </user>
</members>

Any help will be appreciated.

  • 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-05-20T11:19:19+00:00Added an answer on May 20, 2026 at 11:19 am

    One way to do this is to:

    1. Go through each user element
    2. Add the new groups element
    3. Add the existing group to the new groups element
    4. Remove the original group element

    This approach would be similar to this, provided xml is an XElement:

    foreach (var user in xml.Elements("user"))
    {
        user.Add(new XElement("groups", user.Element("group")));
        user.Element("group").Remove(); 
    }
    

    If you’re using an XDocument you could use xml.Root.Elements("user") instead.


    EDIT: in response to your comment, if the XML contained multiple numbered groups you could filter on all elements that start with “group” and do almost the same thing.

    foreach (var user in xml.Elements("user"))
    {
        var groups = user.Elements()
                         .Where(e => e.Name.LocalName.StartsWith("group"))
                         .ToArray();
    
        // rename groups
        foreach (var group in groups)
            group.Name = "group";
    
        user.Add(new XElement("groups", groups));
        groups.Remove();
    }
    

    Notice that I used ToArray() to prevent the Remove call from reevaluating the expression, which would incorrectly remove the newly added groups element since it too matches the condition of starting with “group.” Another way around this would be to change the Where predicate to also check that the name ends with a digit. It’s extra work to prevent accidentally selecting any other element that may start with “group” but it’s up to you based on your knowledge of the XML structure.

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

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
This could be a duplicate question, but I have no idea what search terms
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.