IF XML Node = Variable


Hope someone can help, I’m tying to create an IF statement to say:

[pre]IF ($c -eq $nPrice)
} Else {

$c and $nPrice are both the same value but the result is always No, i suspect its because they are 2 different data types?

$c is a token from a csv file

$nPrice is a node (if that’s the correct term) from an XML response

Can you help me understand what i am doing wrong please.



Importing from CSV or XML (not Import-CliXml) would both be strings. Powershell will attempt to do conversions to numeric values if it needs to for math or comparisons without having to cast. You can use .GetType() to determine what type those variable are:

'$c: {0}' -f $c

'$nPrice: [0}' -f $nPrice

IF ($c -eq $nPrice)
} Else {

Without seeing the variable values, examples (xml, csv), it’s going to be difficult to help you troubleshoot further


Thank you for your reply, sorry i meant to add the values to the first post, they are currency values but without £ such as 160.49 and 160.28

The csv file is made up of 3 columns separated by a pipe:

Part 1|“Part 1 Description”|160.49
Part 2|“Part 2 Description”|160.28

I am trying to check if $c (the price) matches nPrice (The new price from the XML API)

[pre]# Loop through part numbers and prices
Get-Content “UNCPath\fileName.csv” | ForEach {
$a,$b,$c = $_.Split("|")

Create XML Request

$convID = New-Guid
[xml]$xreq = get-content “UNCPath\Template.xml”
$xreq.message.header.conversationID = $convID.ToString()
$xreq.message.body.poAvailabilityRequest.partNo = $a

Send XML request and get response

$response = Invoke-RestMethod -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo)} ‘URL.com/API’ -Method Post -Body $xreq -ContentType ‘text/xml’
$nPrice = $response.SelectSingleNode("//salesPrice")

IF ($c -eq $nPrice)
} Else {

If you check $nPrice, it’s probably still an object like #text. Using the xml in this blog, if we get the first node for AddressPrefix:

PS C:\WINDOWS\system32> $xml.SelectSingleNode('//AddressPrefix')


PS C:\WINDOWS\system32> $xml.SelectSingleNode('//AddressPrefix').InnerText

Another method is using dot notation to get to the string value:

PS C:\WINDOWS\system32> $xml.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.VirtualNetworkSite.AddressSpace.AddressPrefix

Thank you Rob, dot notation worked perfect, I can’t believe i didn’t try that as i was using dot notation before but removed it when troubleshooting an earlier error that i thought was attributed to that.

Thank you for your support.