Understanding ForEach loops and use variables

I am new to PowerShell and need to understand why this does not work:

This first example works the way I want.

Function Get-FileName($initialDirectory){  
 [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = “All files (*.*)| *.*”
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} #end function Get-FileName

$Document = [xml]''
 Write-Host "Please select the Actual file."
 $ActualFile = Get-FileName -initialDirectory “$HOME\Downloads” # This function gets the file name of the file you want to create an object.

[ xml ]$Document = Get-Content $ActualFile

Foreach($TechRcrdId in $Document.BizData.Pyld.Document.SctiesFincgRptgTxRpt.TradData.Rpt.Err.TechRcrdId){
       Write-Host "TechRcrdId :" $TechRcrdId  
      }

This does not do what I want but I want to use a variable for the path.

$Document = [xml]''

 Write-Host "Please select the Actual file."
 $ActualFile = Get-FileName -initialDirectory “$HOME\Downloads” # This function gets the file name of the file you want to create an object.

 [ xml ]$Document = Get-Content $ActualFile
 $NodePath = "BizData.Pyld.Document.SctiesFincgRptgTxRpt.TradData.Rpt.Err.TechRcrdId"

Foreach($TechRcrdId in $Document.$NodePath){
       Write-Host "TechRcrdId :" $TechRcrdId  
      }

Kenneth,
Welcome to the forum. :wave:t3:

Before we proceed … could you please go back, edit your question once again and fix the formatting of your code?
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

I googled for you … this might help:

And BTW: The function Get-FileName is for your question completely irrelevant. You should have omit it. :wink:

That google search shows how to split out each node in the chain. I am looking for a way to use a variable to replace the dot path. Also I don’t really have namespaces in the XML.

Hmm ok … how about this?

[xml]$Document = Get-Content -Path 'path to XML File.xml'

$TechRcrdIdList = $Document.BizData.Pyld.Document.SctiesFincgRptgTxRpt.TradData.Rpt.Err.TechRcrdId

Foreach ($TechRcrdId in $TechRcrdIdList ) {
    Write-Host "TechRcrdId :" $TechRcrdId  
}

Why do you actually want to do that with a variable?

Just starting out with PowerShell so I am taking baby steps.
What I want to do is better a compare tool built in PowerShell that does not order the repeatable field (could be 1 or Many) so the compare fails.
It might be better for me to build my own compare tool that compares true XML as the current one just breaks up the XML the compares based on the trade strings. I am trying to find a way to order the XML so the strings will compare and not give false positives.
My xml paths will be many as my documents can have hundreds of trades so I think making my Xpath a variable is the best way to go.

OK. I am not an XML expert at all but how would it help you to have the path in a variable?