Search code examples
xpathautomationxml-parsingload-testingpowershell-4.0

How to use XML - SelectNode on a .loadtest file in powershell


I'm trying to read a .loadtest file (Visual Studio Load Test File (.loadtest)) from a powershell to try and change an attribute of a node in the .loadtest file.

Although the file is a .loadtest file, its basically an xml file

the .loadtest file is like this:

<?xml version="1.0" encoding="utf-8"?>
<LoadTest Name="ABC" Description="" Owner="" storage="c:\users\....\ABC.loadtest" Priority="2147483647" Enabled="true" CssProjectStructure="" CssIteration="" DeploymentItemsEditable="" WorkItemIds="" TraceLevel="None" CurrentRunConfig="Run Settings1" Id="XXXX-XXXX....XXXX" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Scenarios>
    <Scenario Name="ABC" DelayBetweenIterations="0" PercentNewUsers="100" IPSwitching="true" TestMixType="PercentageOfUsersRunning" ApplyDistributionToPacingDelay="true" MaxTestIterations="0" DisableDuringWarmup="false" DelayStartTime="0" AllowedAgents="">
      <ThinkProfile Value="0.2" Pattern="NormalDistribution" />
      <LoadProfile Pattern="Step" InitialUsers="10" MaxUsers="1000" StepUsers="10" StepDuration="10" StepRampTime="0" />
...
...
...
<RunConfigurations>
  <RunConfiguration Name="Run Settings1" RunDuration="43200" SampleRate="15">
  </RunConfiguration>
</RunConfigurations>

Im using the code:

$Content = [XML]( Get-Content -Path $Path)
$Node = $Content.SelectNodes("/LoadTest/Scenarios/Scenario/LoadProfile");

also tried $Content.SelectNodes("/LoadTest/RunConfigurations/RunConfiguration");

SelectNodes is working fine for app configs and xmls but it isn't working for this kind of file. If I check for the $Content, it displays

[DBG]: PS C:\Users\a>> $Content

 xml                                            LoadTest 
-----                                         ------------
version="1.0" encoding="utf-8"                 LoadTest   

So this is a valid file but it isnt letting me use SelectNode If I do the below, there's no output nothing:

[DBG]: PS C:\Users\a>> $Content.SelectNodes("/LoadTest");

Other details:

PS Version: 4

PS: I checked the path, the content, my commands and everything seems nice. If I just replace this with an app config file, it works absolutely fine but has an issue with this file.


Solution

  • So after lot of trials, this got it working:

    $Nodes = $Content.SelectNodes("//*") | ? {$_.LocalName -eq "LoadProfile"}
    

    The reason this worked and a simple .SelectNode("/Loadtest") or .SelectNode("//") didn't work is for some reason, reading is not happening for "Loadtest" node as the Loadtest node contains attributes(as provided in the question above). So no matter how I tried (even tried to convert this .loadtest file to .xml and then to get node) it didn't work. I saw this select nodes get all and tried and it worked !