.SelectSingleNode in Powershell script using xPath not working on extracting values from web.config file
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)
Other alternative can be by using Select-Xml cmdlet:
$nameSpace = @{ x=$WebConfigXml.DocumentElement.NamespaceURI } $result = Select-Xml -Xml $WebConfigXml -XPath "//ns:add[@key='SiteDomain']" -Namespace $nameSpace