.SelectSingleNode in Powershell script using xPath not working on extracting values from web.config file

倖福魔咒の 提交于 2019-11-28 01:12:37

Your XML file has a namespace:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

so you need a namespace manager for SelectSingleNode (see section "Remarks"):

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!