2016-02-09 27 views
1

我試圖從一個文檔到另一個導入XML節點。我使用兩個參數 - 節點和布爾參數在XmlDocument上調用ImportNode方法。從調用.NET的PowerShell法布爾參數

... 
$connectionString = $anotherWebConfig.SelectSingleNode("//add[@name='ConnectionString']") 
$WebConfig.ImportNode($connectionString, $True) 
$WebConfig.SelectSingleNode("//connectionStrings").AppendChild($connectionString) 

,但我得到一個錯誤

Exception calling "AppendChild" with "1" argument(s): "The node to be inserted is from a different document context." 

我知道一定有什麼不對的進口。我試圖將$ True參數更改爲1,0,10,並將其刪除,但仍然無濟於事。我很驚訝,即使我用無效參數調用這個方法,它也會毫無例外地通過。

什麼是從布爾參數的PowerShell調用.NET方法的正確方法?

回答

1

你似乎已經在這裏誤診的問題 - 這不是布爾的說法是這樣的問題,但事實上,你嘗試追加節點,而不是進口的節點:

$WebConfig.ImportNode($connectionString, $True) # <-- great effort to import node 
$WebConfig.SelectSingleNode("//connectionStrings").AppendChild($connectionString) 
#                 ^
#                  | 
#              Yet still appending the original 

分配進口節點(從ImportNode()返回)給一個變量,並參考該作爲的參數AppendChild()代替:

$ImportedNode = $WebConfig.ImportNode($connectionString, $True) 
$WebConfig.SelectSingleNode("//connectionStrings").AppendChild($ImportedNode) 
+0

比你!我沒有注意到這個方法返回了一些東西。 –