I have made this example XML to loop through and I am interested in outputting the product name for each item.
<example>
<item>
<productname>Hoover</productname>
</item>
<item>
<productname>TV</productname>
</item>
<item>
<productname>Microwave</productname>
</item>
<item>
<productname>Computer</productname>
</item>
</example>
In my powershell, I can easily loop through each item, but I am unable to select the contents of "productname" and I am unsure why. Here is my example powershell code:
[xml] $xml = Get-Content "./xmlexample.xml"
foreach ($item in $xml.example) {
Write-Host($item.InnerXML) #Outputs all XML as expected
Write-Host($item.InnerText) #Outputs the text of all child elements as expected
Write-Host($item.productname.InnerText) #Does not output anything
Write-Host($item.productname.InnerXML) #Does not output anything
}
Any help or advice as to why this doesn't work would be appreciated. It's acting like a child element with no further children is not treated the same way.
foreach ($item in $xml.example.ChildNodes)