# Read xml file and get value of required node

**URL:** https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909
**Category:** PowerShell Help
**Created:** [September 22, 2023, 2:36am UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909 "2023-09-22T02:36:51Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![JRDumont](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JRDumont](https://forums.powershell.org/u/JRDumont)
#### Post date: [September 22, 2023, 2:36am UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909/1 "2023-09-22T02:36:51Z")

</div>

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.

```auto
<?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.

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

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [September 22, 2023, 6:40am UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909/2 "2023-09-22T06:40:46Z")

</div>

James,  
Welcome back to the forum. 👋🏼 … long time no see. 🙂

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

---

<div class="post-metadata">

### Author: ![JRDumont](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JRDumont](https://forums.powershell.org/u/JRDumont)
#### Post date: [September 22, 2023, 10:19am UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909/3 "2023-09-22T10:19:07Z")

</div>

Hi Olaf,

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

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [September 22, 2023, 10:41am UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909/4 "2023-09-22T10:41:07Z")

</div>

> [@JRDumont](#):
>
> I corrected the code as required.

… looks great now. 👍🏼

… this could work:

```auto
$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

```

---

<div class="post-metadata">

### Author: ![JRDumont](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JRDumont](https://forums.powershell.org/u/JRDumont)
#### Post date: [September 22, 2023, 10:55am UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909/5 "2023-09-22T10:55:07Z")

</div>

It did, thank you.

This is what I get now.

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

```auto
 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

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [September 22, 2023, 11:05am UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909/6 "2023-09-22T11:05:23Z")

</div>

> [@JRDumont](#):
>
> how do I check for each value?

You use the _dot notation_

```auto
$Result.DXF

```

---

<div class="post-metadata">

### Author: ![JRDumont](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JRDumont](https://forums.powershell.org/u/JRDumont)
#### Post date: [September 22, 2023, 11:13am UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909/7 "2023-09-22T11:13:51Z")

</div>

OMG!!!  
Thank you so much!!!

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/read-xml-file-and-get-value-of-required-node/22909/8 "2024-05-16T20:14:41Z")

</div>


