I am attempting to read from an XML file and make an output of the information.
<?xml version="1.0" encoding="utf-8"?>
<ModelList>
<Model name="ThinkCentre M715Q">
<Types>
<Type>10M4</Type>
<Type>10RA</Type>
<Type>10RB</Type>
<Type>10M5</Type>
<Type>10RC</Type>
<Type>10M2</Type>
<Type>10RD</Type>
<Type>10M3</Type>
</Types>
</Model>
</ModelList>
I managed to get the output of the model attribute with the following powershell code.
[xml]$xmlFile = Get-Content -Path C:\Temp\data.xml
$xmlFile.GetType().Attributes
$xmlFile.ModelList.Model | Format-Table
Output information with the current code above:
name Types
---- -----
ThinkCentre M715Q Types
But... As you can see, the Types attribute is just types. I also want to be able to read the nested information inside of the ModelList. I want the output to be more like this:
name Types
---- -----
ThinkCentre M715Q 10M4, 10RA, 10RB, 10M5, 10RC...
I am stuck here. I need guidance to just simply bind the attributes. That Types attribute knows it is associated with the Model of ThinkCentre. Any help is appreciated! Thanks.
Since your XML could contain more models and types, you would need to loop over the nodes.
Also, there is a better way to load the xml than using Get-Content
that automatically takes the documents encoding into account:
# load the xml file. This way, you are ensured to get the file encoding correct
$xml = [System.Xml.XmlDocument]::new()
$xml.Load('C:\Temp\data.xml')
foreach ($model in $xml.ModelList.Model) {
[PsCustomObject]@{
Model = $model.Name
Types = $model.Types.Type -join ', '
}
}
Output:
Model Types
----- -----
ThinkCentre M715Q 10M4, 10RA, 10RB, 10M5, 10RC, 10M2, 10RD, 10M3
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments