Greetings!
I have an XElement object that contains the following:
<Root>
<SubSections>
<SubSection id="A">
<Foo id="1">
<Bar />
<Bar />
<Bar />
</Foo>
<Foo id="2">
<Bar />
<Bar />
</Foo>
<Foo id="3">
<Bar />
</Foo>
</SubSection>
<SubSection id="B">
<Foo id="4">
<Bar />
<Bar />
<Bar />
</Foo>
<Foo id="5">
<Bar />
<Bar />
</Foo>
</SubSection>
<SubSection id="C">
</SubSection>
</SubSections>
</Root>
I’d like to move Foo’s 2 and 3 to the SubSection with the id of “C” such that the result is:
<Root>
<SubSections>
<SubSection id="A">
<Foo id="1">
<Bar />
<Bar />
<Bar />
</Foo>
</SubSection>
<SubSection id="B">
<Foo id="4">
<Bar />
<Bar />
<Bar />
</Foo>
<Foo id="5">
<Bar />
<Bar />
</Foo>
</SubSection>
<SubSection id="C">
<Foo id="2">
<Bar />
<Bar />
</Foo>
<Foo id="3">
<Bar />
</Foo>
</SubSection>
</SubSections>
</Root>
What’s the best way to go about moving Foo sections “2” and “3” to the “C” SubSection?
You need to get Foo sections 2 and 3 with a query like:
And then iterate that list and remove them from their parents with
Then just add them to the correct node with:
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:
After that your xDoc should have the correct tree.