Read xml file and get value of required node

Hello,

I have a power shell script that reads an XML file and I am having issues getting just the value of one of the nodes.

Below is a snippet of the XML file.

<?xml version="1.0" encoding="utf-8"?>
<file_type>
		<FName>
			<name>511</name>
			<DXF>True</DXF>
			<STL>True</STL>
			<STP>False</STP>
		</FName>
		<FName>
			<name>600</name>
			<DXF>False</DXF>
			<STL>True</STL>
			<STP>True</STP>
		</FName>
		<FName>
			<name>700</name>
			<DXF>True</DXF>
			<STL>False</STL>
			<STP>False</STP>
		</FName>
</file_type>

Below is the script that reads the XML file.

$XMLPath = "C:\temp\AmerluxExportFilter.xml"
[XML]$xml = Get-Content $XMLPath

#This variable is set for testing purposes
$fileNameFirstXChar="511"

#Search XML file for match. If found set export choices for DXF, STP, and STL files.
If ($Xml.file_type.FName | Where-Object { $_.Name -eq $fileNameFirstXChar }) {
    write-host "Found match in XML file... '$($fileNameFirstXChar)'..."
    $DXFChoice= $Xml.file_type.FName | Where-Object {$_.Name -eq $fileNameFirstXChar } | Select-Object -Property DXF
    $STPChoice= $Xml.file_type.FName | Where-Object {$_.Name -eq $fileNameFirstXChar } | Select-Object -Property STP
    $STLChoice= $Xml.file_type.FName | Where-Object {$_.Name -eq $fileNameFirstXChar } | Select-Object -Property STL
    write-host "DXFChoice... '$($DXFChoice)'..."
    write-host "STPChoice... '$($STPChoice)'..."
    write-host "STLChoice... '$($STLChoice)'..."
    }
    else {
    write-host "NO Found match in XML file for... '$($fileNameFirstXChar)'..."
    }

The output is shown below.

Found match in XML file… ‘511’…
DXFChoice… ‘@{DXF=True}’…
STPChoice… ‘@{STP=True}’…
STLChoice… ‘@{STL=False}’…

I am not sure what data type is being output.
I only need the value, i.e. True or False and it would be great if it could be boolean.

I’m open to suggestions or best practices.

Thank you in advance.

James,
Welcome back to the forum. :wave:t3: … long time no see. :slightly_smiling_face:

As you might noticed the forum software messed up with some of your code. And it is hard to distinguish between code, sample data and prosa. So could you please go back, edit your question once again and fix the formatting of your code and sample data?

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: )

1 Like

Hi Olaf,

Thank you for responding.
I corrected the code as required.

… looks great now. :+1:t3:

… this could work:

$Result =
If ($Xml.file_type.FName | Where-Object { $_.Name -eq $fileNameFirstXChar }) {
    $DesiredNode = 
        $Xml.file_type.FName | 
            Where-Object { $_.Name -eq $fileNameFirstXChar }
    [PSCustomObject]@{
        DXF = if( $DesiredNode.DXF -eq 'true' ){ $true } else { $false }
        STP = if( $DesiredNode.STP -eq 'true' ){ $true } else { $false }
        STL = if( $DesiredNode.STL -eq 'true' ){ $true } else { $false }
    }
    }
$Result
1 Like

It did, thank you.

This is what I get now.

PS C:\ProgramData\coolOrange\powerJobs\Jobs> powershell.exe -File testreadxml.ps1

 DXF  STP   STL
 ---  ---   ---
True True False

Can I impose a bit more to find out how to use the data? It looks like the three values are stored in $Result so how do I check for each value?

For example, I have to execute code if DXF = true

You use the dot notation

$Result.DXF

OMG!!!
Thank you so much!!!