Get data from multiple XML files

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Hal242
Posts: 12
Last visit: Thu Apr 30, 2020 9:09 pm

Get data from multiple XML files

Post by Hal242 »

Hello, I have many XML files in a folder. Each XML file has a different name, but all have the same XML schema. I can get values from different nodes in any of the XML files using the following code;

Code: Select all

[xml]$xmlDocument = Get-Content -Path D:\TEMP\FileName1.xml
$xmlDocument.HardwareVerificationData.runtime.ChildNodes
$xmlDocument.HardwareVerificationData.Hardware.CPUID.ChildNodes
$xmlDocument.HardwareVerificationData.Hardware.SMBIOS.System.ChildNodes
$xmlDocument.HardwareVerificationData.Hardware.SMBIOS.System.p[4]
$xmlDocument.HardwareVerificationData.Hardware.SMBIOS.Baseboard.ChildNodes
$xmlDocument.HardwareVerificationData.Hardware.SMBIOS.Baseboard.p[4, 5]
	
etc...
The thing I cannot figure out is how to write code which will look at all of the files in my directory, say D:\temp instead of hardcoding the FileName1.xml in the first get-content command. I imagine I need to create some sort of loop to iterate through the folder and setup the get-content command to use a variable instead of the FileName1.xml and then loop through all of the files in the directory and use the dot notation similar to above to build out my report.

I am not familiar with XPath, and some searching I have done suggests that XPath is the old school way to do it (from what I can tell), but I am sure there is an easier way.

Any suggestions would be greatly appreciated.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get data from multiple XML files

Post by jvierra »

Start with:

Help ForEach-Object -online

Example:

Code: Select all

Get-ChildItem *.xml |
     ForEach-Object{
           # process each file
           [xml]$xml = Get-Content $_
           …. other code     }
{/code]

Here are some PowerShell videos that will help you get started with PS and files.

https://youtu.be/6VK4TN6Umfk
Hal242
Posts: 12
Last visit: Thu Apr 30, 2020 9:09 pm

Re: Get data from multiple XML files

Post by Hal242 »

thanks... I knew I was getting close and your reply helped me get to the solution.

this seems to do the trick;

Code: Select all

Get-ChildItem D:\temp -filter *.xml | ForEach-Object {
[xml]$xmlDocument = Get-Content -path d:\temp\$_
$xmlDocument.HardwareVerificationData.runtime.ChildNodes
$xmlDocument.HardwareVerificationData.Hardware.SMBIOS.Chassis.ChildNodes
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get data from multiple XML files

Post by jvierra »

You don't need to want to use a path structure. THe code I psoted is the correct wway to do this in PS. Whenyou learn more you will understand why.

Code: Select all

Get-ChildItem D:\temp\*.xml | 
    ForEach-Object {
        [xml]$xmlDocument = Get-Content $_
        $xmlDocument.HardwareVerificationData.runtime.ChildNodes
        $xmlDocument.HardwareVerificationData.Hardware.SMBIOS.Chassis.ChildNodes
    }
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked