Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy from one xml file to another - powershell

Tags:

powershell

xml

Zdravo! Now this one has been bugging me quite a lot... I'm trying to copy a couple of nodes from one XML file to another using powershell, and looked around the whole internets, but it seems that only on my pc the universal solution to the problem is not working. So this is the code I've got now:

#loading one document
$xml = New-Object XML
$xml.Load("C:\Inetpub\WWWroot\web.config")

#load another
$wconfig = New-Object XML
$wconfig.Load("C:\Users\foo\test.enc")

#now do what everyone on the internet says
$xml.DocumentElement.InsertAfter($wconfig.ImportNode($wconfig.SelectSingleNode($node), $true), $afterNode))

Anyone got any idea why this (and a couple of other alternatives like .Clone()) always return "The node to be inserted is from a different document context." Or just give me another alternative. Anything that works :D

Thanks

like image 645
kalbsschnitzel Avatar asked Jun 08 '12 13:06

kalbsschnitzel


1 Answers

I think it should be $XML.ImportNode($wconfig.SelectSingleNode ... blah instead of $wconfig.ImportNode($wconfig.SelectSingleNode

like image 157
David Brabant Avatar answered Oct 16 '22 12:10

David Brabant