Ciaran Donoghue
Ciaran Donoghue

Reputation: 828

Can't hold the value of SelectNode in a variable from a xml document

I am using to parse the data of an XML document that I have created, so far everything is working like this:

$xdoc.SelectNodes("//key")[0]
$xdoc.SelectNodes("//value")[0]
$xdoc.SelectNodes("//key")[1]
$xdoc.SelectNodes("//value")[1]
$xdoc.SelectNodes("//key")[2]
$xdoc.SelectNodes("//value")[2]

All this works fine but I want to parse the values into a variable like this:

$a1 = $xdoc.SelectNodes("//key")[0]
$a2 = $xdoc.SelectNodes("//value")[0]
$b1 = $xdoc.SelectNodes("//key")[1]
$b2 = $xdoc.SelectNodes("//value")[1]
$c1 = $xdoc.SelectNodes("//key")[2]
$c2 = $xdoc.SelectNodes("//value")[2]

Then if I try Write-Host $a1 it will not display the value, how can I get the variable to hold this value. I tried using SelectSingleNode but got the same results. I tried using .InnerText, .InnerHtml, .Value all of these will display correctly without using the $variables so how can I hold the information in a variable.

My xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xml:space="preserve">
<triggers>
<defined>true</defined>
<triggerDefinition>
<id>1</id>
<name>After successful build plan</name>
<userDescription>Auto release</userDescription>
<isEnabled>true</isEnabled>
<pluginKey>com.atlassian.bamboo.triggers.atlassian-bamboo-triggers:afterSuccessfulPlan</pluginKey>
<triggeringRepositories/>
<config>
<item>
<key>deployment.trigger.afterSuccessfulPlan.triggeringBranch</key>
<value>INT-LOAN</value>
</item>
<item>
<key>deployment.trigger.afterSuccessfulPlan.triggeringPlan</key>
<value>INT-LOAN</value>
</item>
<item>
<key>deployment.trigger.afterSuccessfulPlan.branchSelectionMode</key>
<value>INHERITED</value>
</item>
</config>
</triggerDefinition>
</triggers>
<bambooDelimiterParsingDisabled>true</bambooDelimiterParsingDisabled>
</configuration>

Upvotes: 0

Views: 147

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 59001

You have to select a subnode / attribute for your selected node. For example, if you want to output the Id attribute value using Write-Host:

Write-Host $a1.Id

Edit:

I copied your XML, invoking this actual outputs deployment.trigger.afterSuccessfulPlan.triggeringBranch:

[xml]$xdoc = Get-Content 'your_xml_path'
$a1 = $xdoc.SelectNodes("//key")[0]
Write-Host $a1.InnerText

Upvotes: 1

Related Questions