I have slightly diff xml from normal xml like below, xmlns is not at the root level, it is 2 level depth from the root, for which i'm unable to use below solution. Can anyone please help me select the node. Thanks in advance.
I'm unable to use below snippet to select the node
$ns = New-Object System.Xml.XmlNamespaceManager($WebConfigXml.NameTable)
$ns.AddNamespace("ns", $WebConfigXml.DocumentElement.NamespaceURI)
$node = $WebConfigXml.SelectSingleNode("//ns:add[@key='SiteDomain']", $ns)
XML :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<app1>
</app1>
<assembly>
<runtime xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<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>
</runtime>
</assembly>
</configuration>
Your namespace is not initialized correctly in $ns
. You need to drill down to the node that contains the namespace definition:
$ns.AddNamespace('ns',$WebConfigXml.configuration.assembly.runtime.NamespaceURI)