从一个xml文件复制到另一个powershell

时间:2022-05-10 15:42:55

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:

Zdravo !这个问题一直困扰着我……我正在尝试使用powershell将几个节点从一个XML文件复制到另一个XML文件,并查看整个internet,但似乎只有在我的pc上,解决这个问题的通用解决方案才会不起作用。这就是我现在的代码:

#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

任何人都知道为什么这个(以及其他几个替代方法,如. clone()))总是返回“要插入的节点来自不同的文档上下文”。或者给我另一个选择。任何工作原理:D

Thanks

谢谢

1 个解决方案

#1


6  

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

我认为应该是$XML.ImportNode($wconfig)。SelectSingleNode……而不是wconfig.ImportNode美元($ wconfig.SelectSingleNode

#1


6  

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

我认为应该是$XML.ImportNode($wconfig)。SelectSingleNode……而不是wconfig.ImportNode美元($ wconfig.SelectSingleNode