2011-04-12 67 views
1
$xmlFile = "D:\ServiceConfiguration.cscfg" 
    [xml]$doc = Get-Content $xmlFile 
    $node = $doc.SelectSingleNode("/ServiceConfiguration/Role/ConfigurationSettings[@name='DiagnosticsConnectionString']") 
    $node.value = "New-Value" 
    $doc.Save($xmlFile) 

SelectSingleNode總是返回null。請幫助電源外殼腳本SelectSingleNode不工作

+1

你的XPath是錯誤的或者有在cscfg文件中使用的命名空間。沒有輸入文件很難回答.. – stej 2011-04-12 11:59:21

回答

5

的元素是命名空間限定的,所以你需要在查詢中指定命名空間:

$xmlFile = "D:\ServiceConfiguration.cscfg" 
[xml]$doc = Get-Content $xmlFile   
$ns = new-object Xml.XmlNamespaceManager $xml.NameTable 
$ns.AddNamespace('dns', 'http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration') 
$node = $doc.SelectSingleNode("/dns:ServiceConfiguration/dns:Role/dns:ConfigurationSettings[@name='DiagnosticsConnectionString']", $ns)  
$node.value = "New-Value" 
$doc.Save($xmlFile)