IF XML Node = Variable

Welcome Forums General PowerShell Q&A IF XML Node = Variable

Viewing 4 reply threads
  • Author
    Posts
    • #194399
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      Hi,

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

      IF ($c -eq $nPrice)
      {
      ‘Yes’
      } Else {
      ‘No’
      }

      $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.

      Regards,

      Jamie

    • #194426
      Participant
      Topics: 13
      Replies: 1683
      Points: 2,849
      Helping Hand
      Rank: Community Hero

      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
      $c.GetType()
      
      '$nPrice: [0}' -f $nPrice
      $nPrice.GetType()
      
      IF ($c -eq $nPrice)
      {
      'Yes'
      } Else {
      'No'
      }
      

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

    • #194435
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      Hi,

      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)

      # 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)
      {
      ‘Yes’
      } Else {
      ‘No’
      }
      }
    • #194441
      Participant
      Topics: 13
      Replies: 1683
      Points: 2,849
      Helping Hand
      Rank: Community Hero

      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')
      
      #text         
      -----         
      192.168.1.0/24
      
      
      PS C:\WINDOWS\system32> $xml.SelectSingleNode('//AddressPrefix').InnerText
      192.168.1.0/24
      

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

      PS C:\WINDOWS\system32> $xml.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.VirtualNetworkSite.AddressSpace.AddressPrefix
      
      192.168.1.0/24
      
    • #194447
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      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.

Viewing 4 reply threads
  • The topic ‘IF XML Node = Variable’ is closed to new replies.