【问题标题】:.SelectSingleNode in Powershell script using xPath not working on extracting values from web.config file.SelectSingleNode 在 Powershell 脚本中使用 xPath 无法从 web.config 文件中提取值
【发布时间】:2021-03-13 15:05:45
【问题描述】:

好的,这是我的 web.config 文件的 sn-p:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<location path="." inheritInChildApplications="false">
<connectionStrings>
...
</connectionStrings>
</location>
<location path="." inheritInChildApplications="false">
<appSettings>
<!--IT Ops-->
<add key="SomeOtherKey" value="SomeOtherValue" />
<add key="SiteDomain" value="somedomain.com" />
<add key="SomeOtherKey" value="SomeOtherValue" />
....
</appSettings>
</location>
</configuration>

我要做的是通过 Powershell 使用 xPath 找到节点。关于这个 XML 文件有几点需要注意:

有多个:

<location path="." inheritInChildApplications="false"> 

xml 文件中的值。它们围绕着其他节点,如 etc...

我可以使用此脚本成功找到并替换连接字符串值

$WebConfigFile = Join-Path $destination Web.config
[xml]$WebConfigXml = Get-Content ($WebConfigFile)
$WebConfigXml.configuration.location[2].connectionStrings.add | % { $_.connectionString = $_.connectionString -replace "some value", $sqlServerName }

但是当我去使用这个脚本替换 add key="SiteDomain" 值时:

$node = $WebConfigXml.configuration.location[3].appSettings.SelectSingleNode("add[@key = 'SiteDomain']")
$node.value = "someValue"
$WebConfigXml.Save($WebConfigFile)

它不起作用。本例中的 $node 值包含一个空字符串。

我也在尝试像这样读取节点:

$appSettingsSection = $WebConfigXml.configuration.location[3].appSettings;
$existingSiteDomain = $appSettingsSection.SelectSingleNode("add[@key='SiteDomain']")

我仍然得到 $existingSiteDomain 值的空字符串。

我查看了使用 SelectSingleNode 的示例,但我似乎不太明白。不太清楚我做错了什么。

谢谢, 迈克

【问题讨论】:

    标签: powershell xpath web-config selectsinglenode


    【解决方案1】:

    您的 XML 文件有一个命名空间:

    &lt;configuration <b>xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"</b>&gt;

    所以你需要一个SelectSingleNode 的命名空间管理器(参见“备注”部分):

    XPath 表达式可以包含命名空间。使用 XmlNamespaceManager 支持命名空间解析。如果 XPath 表达式包含前缀,则必须将前缀和命名空间 URI 对添加到 XmlNamespaceManager

    这样的事情应该可以工作:

    $ns = New-Object System.Xml.XmlNamespaceManager($WebConfigXml.NameTable)
    $ns.AddNamespace("ns", $WebConfigXml.DocumentElement.NamespaceURI)
    $node = $WebConfigXml.SelectSingleNode("//ns:add[@key='SiteDomain']", $ns)
    

    【讨论】:

    • 好的,我试过了,但现在我从 Powershell 收到一个错误。它不知道 XmlNamespaceManager 是:“XmlNamespaceManager : 术语 'XmlNamespaceManager' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是正确的,然后再试一次。”。我在 powershell 命令行上运行了“$psversiontable.psversion”,它说我安装了 Powershell 3.0 版。这是某种类型的版本控制问题吗?
    • 好的,我修改我之前的评论。它现在正在工作。我认为我的脚本中还有其他一些语法问题导致了这个错误。您提供的代码有效!非常感谢。我是 Powershell 和 xPath 的新手,所以我不知道这个命名空间问题。
    【解决方案2】:

    其他替代方法可以使用Select-Xml cmdlet:

    $nameSpace = @{ x=$WebConfigXml.DocumentElement.NamespaceURI }    
    $result = Select-Xml -Xml $WebConfigXml -XPath "//ns:add[@key='SiteDomain']" -Namespace $nameSpace
    

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多