c# - Powershell 脚本 SelectSingleNode 不工作

标签 c# .net 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)

SelectSingleNode 总是返回 null。请帮忙

最佳答案

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

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

关于c# - Powershell 脚本 SelectSingleNode 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5634584/

相关文章:

.net - 有没有办法将压缩文件放在与压缩文本文件相同的文件夹中?

powershell - 将单个文件复制到不存在的文件夹

c# - 在 MVC Controller 中打开一个 websocket channel

C# 在 Application.Run() 之后挂起并卡在 for 循环中

c# - WPF Canvas 添加到背景

.net - 如何在 VB.NET 中设置字体类对象的颜色?

c# - 水平滚动条在 DataGridView 上不可见

javascript - 将 Index.cshtml JavaScript 数组移动到单独的脚本文件中

c# - 如何使用 MAPI 获取 Outlook 联系人组?

powershell - PowerShell 中哈希表的并集和交集