Power shell脚本SelectSingleNode无法正常工作

SRA*_*SRA 1 .net c# powershell

   $xmlFile = "D:\ServiceConfiguration.cscfg"  
   [xml]$doc = Get-Content $xmlFile 
   $node = $doc.SelectSingleNode("/ServiceConfiguration/Role/ConfigurationSettings[@name='DiagnosticsConnectionString']") 
   $node.value = "New-Value" 
   $doc.Save($xmlFile)
Run Code Online (Sandbox Code Playgroud)

SelectSingleNode始终返回null.请帮忙

Kei*_*ill 6

元素是名称空间限定的,因此您需要在查询中指定名称空间:

$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)  
Run Code Online (Sandbox Code Playgroud)