使用boolean参数从PowerShell调用.NET方法

时间:2016-02-09 11:33:34

标签: xml powershell boolean method-call

我正在尝试将XML节点从一个文档导入另一个文档。我在XmlDocument上使用两个参数调用ImportNode方法 - node和boolean parameter。

...
$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 个答案:

答案 0 :(得分: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)
相关问题