I would like to clone an Xml element, insert it at the end of the list of elements, and save the document. Can someone explain how this is done in linq for 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 Folder element as a virtual folder on disk. I would like to copy the Rock folder to music, so the resulting xml should be lower
Desired Result
<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 in progress
- Clone source node (Done # 1)
- Clone other nodes inside the node source (don't know how to do this # 2)
- Create a new identifier for the nodes inside # 2 and change the path value (I know how to do this)
- Insert 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());
2
var directChildren = (from folder in _xmlDataSource.Descendants("Folders").Descendants("Folder") where folder.Attribute("PathValue").Value.Contains(sourcePathValue) select folder);
Question
Can someone help me with C # 2 and # 4?
source share