You need to get Foo sections 2 and 3 with a query like:
var foos = from xelem in root.Descendants("Foo")
where xelem.Attribute("id").Value == "2" || xelem.Attribute("id").Value == "3"
select xelem;
And then iterate that list and remove them from their parents with
xelem.Remove();
Then just add them to the correct node with:
parentElem.Add(xelem);
The first query will get you both sections then remove and add each one to the correct place on the tree.
Here's a complete solution:
var foos = (from xElem in xDoc.Root.Descendants("Foo")
where xElem.Attribute("id").Value == "2" || xElem.Attribute("id").Value == "3"
select xElem).ToList();
var newParentElem = (from xElem in xDoc.Root.Descendants("SubSection")
where xElem.Attribute("id").Value == "C"
select xElem).Single();
foreach(var xElem in foos)
{
xElem.Remove();
newParentElem.Add(xElem);
}
After that your xDoc should have the correct tree.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…