Select an attribute with Xpath in a XML with namespace using Powershell



I have a .Net config file with a NLog node inside:



<?xml version="1.0"?>
<configuration>
<appSettings>
</appSettings>
<nlog xmlns="http://ift.tt/JmPwGs"
xmlns:xsi="http://ift.tt/ra1lAU">
<targets async="true">
<target xsi:type="File" name="f" fileName="path to file" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="c,f" />
</rules>
</nlog>
</configuration>


I'm trying to change the target's fileName value using Powershell.


I'm using this code:



[xml]$xml = Get-Content $file
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("ns", $namespace) # $namespace is `http://ift.tt/JmPwGs`
$values = $xml.selectNodes($xpath, $ns)


My XPath expression is //ns:nlog/ns:targets/ns:target[@ns:name='f']/@ns:fileName


But $values always has 0 results.


Any idea ? What am I doing wrong ?


Thanks !


No comments:

Post a Comment