I have an XML file, which looks like the following:
<ViewsFile>
<Categories>
<ViewCategory Name="General">
<Views>
<View Name="firstView">
...
</View>
<View Name="secondView">
...
</View>
</Views>
</ViewCategory>
<ViewCategory Name="Bookings">
<Views>
<View Name="firstView">
...
</View>
<View Name="secondView">
...
</View>
</Views>
</ViewCategory>
<ViewCategory Name="Activities">
<Views>
<View Name="firstView">
...
</View>
</Views>
</ViewCategory>
</Categories>
</ViewsFile>
Now I want to append the following XML file to the first one, taking into account the correct position, namely the parent with the name 'Bookings':
<ViewCategory Name="Bookings">
<Views>
<View Name="newView">
...
</View>
</Views>
</ViewCategory>
So the final XML file should look like this:
<ViewsFile>
<Categories>
<ViewCategory Name="General">
<Views>
<View Name="firstView">
...
</View>
<View Name="secondView">
...
</View>
</Views>
</ViewCategory>
<ViewCategory Name="Bookings">
<Views>
<View Name="firstView">
...
</View>
<View Name="secondView">
...
</View>
<View Name="newView">
...
</View>
</Views>
</ViewCategory>
<ViewCategory Name="Activities">
<Views>
<View Name="firstView">
...
</View>
</Views>
</ViewCategory>
</Categories>
</ViewsFile>
My PowerShell script at the moment looks like this:
[xml] $x = Get-Content C:\Users\user\Desktop\exitingViews.xml;
[xml] $y = Get-Content C:\Users\user\Desktop\newView.xml;
$x.ViewsFile.Categories.ViewCategory.Views.AppendChild($x.ImportNode(($y.ViewCategory.Views.View), $true));
$x.Save('C:\Users\user\Desktop\final.xml');
But it only appends the new view to the last category.
Is there any 'easy' possibility to define, where to append the child node?