0

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.

1
  • Please try foreach ($item in $xml.example.ChildNodes) Commented Jun 18, 2018 at 14:18

2 Answers 2

2

Iterate over $xml.example.ChildNodes and then the text is just $item.productname:

PS C:\users\IEUser\Documents> foreach ($item in $xml.example.ChildNodes) {
>>>    Write-Host($item.productname)
>>> }
Hoover
TV
Microwave
Computer

Or if you only want item elements containing productname you can iterate the item nodes:

PS C:\users\IEUser\Documents> foreach ($item in $xml.example.item) {
>>>    Write-Host($item.productname)
>>> }
Hoover
TV
Microwave
Computer

Or just extract an array of strings directly:

PS C:\users\IEUser\Documents> $xml.example.item.productname
Hoover
TV
Microwave
Computer
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe you assigned something over $xml and/or $item. My code snippets are direct copy/paste after the [xml] $xml = Get-Content ".\xmlexample.xml"
I think I messed something up. I have your code working now - I think I never went into the 'item' element, which is why it wasn't working for me. Your examples were very clear and useful, thank you!
1

Powershell xml maps the leafs of an xml document as properties. Also, similar like in Linq-to-XML, each query may return an array of elements. So you can write your loop actually like this:

[xml]$xml = '<example>
<item>
   <productname>Hoover</productname>
</item>
<item>
   <productname>TV</productname>
</item>
<item>
  <productname>Microwave</productname>
</item>
<item>
  <productname>Computer</productname>
</item>
</example>'
$xml.example.item[1].productname = "Foo"
$xml.example.item.productname | Write-Host

Results in:

Hoover
Foo
Microwave
Computer

'productname' acts like a string property which can be set or retrieved. Therefore it has no further xml methods or properties.

1 Comment

To be completely accurate, $xml.example.item.productname is an array and each of its elements is a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.