【问题标题】:Power shell script SelectSingleNode not workingPowershell 脚本 SelectSingleNode 不起作用
【发布时间】:2011-04-12 22:49:06
【问题描述】:
   $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。请帮忙

【问题讨论】:

  • 您的 XPath 错误或 cscfg 文件中使用了命名空间。没有输入文件很难回答..

标签: c# .net powershell


【解决方案1】:

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

$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)  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多