Find the attribute value and replacing with new value of attribute in XML file

Viewed 35

Using Powershell, I want to make changes in XML file and the files that need change 0 to ${ThinkTime} for the name RandomTimer.range in multiple places.

I want to check if 0 is present before replace.

XML message:

<UniformRandomTimer guiclass="UniformRandomTimerGui" testclass="UniformRandomTimer" testname="Think Time" enabled="true">
    <stringProp name="ConstantTimer.delay">${ThinkTime}</stringProp>
    <stringProp name="RandomTimer.range">0</stringProp>
</UniformRandomTimer>

Below Powershell script is not working as expected:

Get-ChildItem -Path C:\Users**\Desktop\Projects**\D****\scripts*.jmx | ForEach-Object { 
    $xmlDocument = [xml]($_ |Get-Content) 
    $tmConfig = $xmlDocument.SelectSingleNode("//UniformRandomTimer/stringProp[2]") 
    # attribute exists, let's update it! 
    if($tmConfig.GetAttribute('#text') -eq "0") { 
        $tmConfig.SetAttribute("#text", "${ThinkTime}") 
    } 
    $xmlDocument.Save($_.FullName) 
}
1 Answers

Your question isn't clear, but try making these changes and see if it works on your actual xml:

$tmConfig = $xml.SelectSingleNode('//UniformRandomTimer//stringProp[.="0"]')
$tmConfig.innerText = "something or other"
Related