How do I clone a xml element using Linq to Xml

Whitesky

New Member
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 xmlXml\[code\] <Folders> <Folder ID="1" Name="Music" PathValue="http://stackoverflow.com/questions/10697566/Root/Music" ParentId="0"></Folder> <Folder ID="2" Name="Rock" PathValue="http://stackoverflow.com/questions/10697566/Root/Rock" ParentId="1"></Folder> </Folders>\[/code\]Contextthink 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 belowResult Required\[code\] <Folders> <Folder ID="1" Name="Music" PathValue="http://stackoverflow.com/questions/10697566/Root/Music" ParentId="0"></Folder> <Folder ID="2" Name="Rock" PathValue="http://stackoverflow.com/questions/10697566/Root/Rock" ParentId="0"></Folder> <Folder ID="3" Name="Rock" PathValue="http://stackoverflow.com/questions/10697566/Root/Music/Rock" ParentId="1"></Folder> </Folders>\[/code\]Operations to be carried out[*]Clone the source node ( Done #1)[*]Clone the other nodes inside source node ( Don't know how to do it #2)[*]Generate new ID for the nodes inside #2 and change pathvalue ( I know how to do this)[*]Insert the node #1 and nodes from #2 ( Don't know)1\[code\]var source = new XElement((from folder in _xmlDataSource.Descendants("Folders").Descendants("Folder") wherewallet.Attribute("ID").Value.Equals(sourceWalletId, StringComparison.OrdinalIgnoreCase) select wallet).First());//source is a clone not the reference to node.\[/code\]2\[code\]var directChildren = (from folder in _xmlDataSource.Descendants("Folders").Descendants("Folder") where folder.Attribute("PathValue").Value.Contains(sourcePathValue) select folder);//How do i clone this\[/code\]QuestionCould someone help me with #2 and #4?
 
Back
Top