I am combining three xml documents into one using C# XML and Linq. Before I want to save it as a "Final.xml" I want to add a <New> node under the parent node and at the bottom before the closing parent node.
I have a XML structure like this:
<Main>
<Action>
<URL />
</Action>
<Execute>
<URL />
<MetaData />
</Execute>
<Action>
<URL />
</Action>
<Assert>
<URL />
</Assert>
</Main>
I want to add a new node under the <Main> node and above the</Main> node. The new Scructure needs to look like this:
<Main>
<New>
<Action>
<URL />
</Action>
<Execute>
<URL />
<MetaData />
</Execute>
<Action>
<URL />
</Action>
<Assert>
<URL />
</Assert>
</New>
</Main>
I've tried code like:
...
var xelem3 = xdoc4.Root.Elements();
xdoc1.Root.LastNode.AddAfterSelf(xelem3);
var Tests = xdoc1.Root.Elements("Test");
foreach (var test in Tests)
{
test.AddBeforeSelf(new XElement("New"));
}
xdoc1.Save(FinalDoc);
This does not work, It runs but nothing happens. I don't think a loop is the best way and I am wondering is there a better way. I have looked around but I don't seem to find what I am looking for.