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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:38:24+00:00 2026-06-04T05:38:24+00:00

I would like to clone a Xml element, insert it to the end of

  • 0

I would like to clone a Xml element, insert it to the end of the element list and save the document. Could someone explain how it is done in linq to xml

Xml

  <Folders>
    <Folder ID="1" Name="Music" PathValue="Root/Music" ParentId="0"></Folder>
    <Folder ID="2" Name="Rock" PathValue="Root/Rock" ParentId="1"></Folder>
  </Folders>

Context

think of the xml element Folder as Virtual folder on disk. I would like to copy the folder Rock into music hence the resulting xml should become as below

Result Required

  <Folders>
    <Folder ID="1" Name="Music" PathValue="Root/Music" ParentId="0"></Folder>
    <Folder ID="2" Name="Rock" PathValue="Root/Rock" ParentId="0"></Folder>
    <Folder ID="3" Name="Rock" PathValue="Root/Music/Rock" ParentId="1"></Folder>
  </Folders>

Operations to be carried out

  1. Clone the source node ( Done #1)
  2. Clone the other nodes inside source node ( Don’t know how to do it #2)
  3. Generate new ID for the nodes inside #2 and change pathvalue ( I know how to do this)
  4. Insert the node #1 and nodes from #2 ( Don’t know)

1

var source = new XElement((from folder in _xmlDataSource.Descendants("Folders").Descendants("Folder") where
wallet.Attribute("ID").Value.Equals(sourceWalletId, StringComparison.OrdinalIgnoreCase) select wallet).First());
//source is a clone not the reference to node.

2

var directChildren = (from folder in _xmlDataSource.Descendants("Folders").Descendants("Folder") where folder.Attribute("PathValue").Value.Contains(sourcePathValue) select folder);
//How do i clone this

Question

Could someone help me with #2 and #4?

  • 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-04T05:38:26+00:00Added an answer on June 4, 2026 at 5:38 am

    You know about the constructor that takes another XElement to create a copy of it, have you tried this?

    var copiedChildren = from folder in _xmlDataSource.Descendants("Folders").Descendants("Folder") 
                         where folder.Attribute("PathValue").Value.Contains(sourcePathValue) 
                         select new XElement(folder);
    

    as you have already cloned source, you can insert those into that node (assuming they should be children of the copied node)

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

Sidebar

Related Questions

I would like to deep clone a List. for that we are having a
I have an XML document that I would like to update after it already
I would like to clone a tag using Javascript (without using any external frameworks
Here is the code .. I would like to clone that code for another
I'm using xmlwriter to create an xml document. The xml document looks like this:
I am reading an XML feed and would like to compare to an old
I would like to display details of an xml error log to a user
I have received some invalid XML data from a poll provider and would like
I have a custom list definition created via visual studio 2010. I would like
I would like to be able to parse XML that isn't necessarily well-formed. I'd

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.