I would like to read and edit node content from a given XML file and write the changed content back.
Content of the given XML file:
<?xml version="1.0" encoding="UTF-8"?>
<SimBase.Document Type="ScenarioFile" version="4,5" id="Test">
<Descr>AceXML Document</Descr>
<Filename>Test.fxml</Filename>
<Flight.Sections>
...
<Section Name="DateTimeSeason">
<Property Name="Season" Value="Summer" />
<Property Name="Year" Value="2020" />
<Property Name="Day" Value="224" />
<Property Name="Hours" Value="12" />
<Property Name="Minutes" Value="2" />
<Property Name="Seconds" Value="17" />
</Section>
...
</Flight.Sections>
</SimBase.Document>
Reading and writing back a new copy of the XML file works fine but I am at total loss how the read / edit the content of the node named 'DateTimeSeason' resp. its nodes since I do not know XPath.
Content of the script that I have written:
Write-Host "Edit XML file"
# Load local functions
. .\LocalFunctions.ps1
# Working environment
$flightFileDir = 'E:\Temp\PowerShell\P3D\'
$flightFileNameIn = 'MauleLSZH.fxml'
$flightFileNameOut = 'MauleLSZHNew.fxml'
$flightFileIn = $flightFileDir + $flightFileNameIn
$flightFileOut = $flightFileDir + $flightFileNameOut
# Get correct date and time for flight file
$dateTimeArr = SetupDateTime
#$dateTimeArr # output content of resulting array
# Set up new XML object and read file content
$xmlFlight = New-Object System.XML.XMLDocument
$xmlFlight.Load($flightFileIn)
# Edit content of node named 'DateTimeSeason'
$data = $xmlFlight.'SimBase.Document'.'Flight.Sections'.Section[@name -eq 'DateTimeSeason'].Property[@name = 'Year']
$data # output for test purposes
# Output modified flight file
$xmlFlight.Save($flightFileOut)
Write-Host "New XML file created"
Thanks a lot for any help or hints Hannes