.SelectSingleNode 在 Powershell 脚本中使用 xPath 无法从 web.config 文件中提取值 [英] .SelectSingleNode in Powershell script using xPath not working on extracting values from web.config file
问题描述
好的,这是我的 web.config 文件的片段:
Okay, so here is the snippet of my web.config file:
<?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 文件的一些注意事项:
What I'm trying to do is find the node using xPath via Powershell. A couple things to note about this XML file:
有多个:
<location path="." inheritInChildApplications="false">
xml 文件中的值.它们围绕着其他节点,如等...
values in the xml file. They surround other nodes like etc...
我可以使用此脚本成功找到并替换连接字符串值
I can find and replace the connection string values successfully using this script
$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" 值时:
But when I go to replace the add key="SiteDomain" value using this script:
$node = $WebConfigXml.configuration.location[3].appSettings.SelectSingleNode("add[@key = 'SiteDomain']")
$node.value = "someValue"
$WebConfigXml.Save($WebConfigFile)
它不起作用.本例中的 $node 值包含一个空字符串.
it does not work. The $node value in this case contains an empty string.
我也想像这样读取节点:
I'm also trying just to read the node like this:
$appSettingsSection = $WebConfigXml.configuration.location[3].appSettings;
$existingSiteDomain = $appSettingsSection.SelectSingleNode("add[@key='SiteDomain']")
而且我仍然得到 $existingSiteDomain 值的空字符串.
And I'm still getting an empty string for the $existingSiteDomain value.
我使用 SelectSingleNode 查看了示例,但似乎不太明白.不太确定我做错了什么.
I've looked at samples using SelectSingleNode and I can't quite seem to figure it out. Not too sure what I'm doing wrong.
谢谢,迈克
推荐答案
你的 XML 文件有一个命名空间:
Your XML file has a namespace:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
因此您需要一个用于 SelectSingleNode
<的命名空间管理器/a>(见备注"部分):
so you need a namespace manager for SelectSingleNode
(see section "Remarks"):
XPath 表达式可以包含命名空间.使用 XmlNamespaceManager 支持命名空间解析.如果 XPath 表达式包含前缀,则必须将前缀和命名空间 URI 对添加到 XmlNamespaceManager.
XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager.
这样的事情应该可以工作:
Something like this should work:
$ns = New-Object System.Xml.XmlNamespaceManager($WebConfigXml.NameTable)
$ns.AddNamespace("ns", $WebConfigXml.DocumentElement.NamespaceURI)
$node = $WebConfigXml.SelectSingleNode("//ns:add[@key='SiteDomain']", $ns)
这篇关于.SelectSingleNode 在 Powershell 脚本中使用 xPath 无法从 web.config 文件中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!