0

I am gathering some XML from a webservice, and I receive it formatted as:

<devices>
  <device>
    <hostname>name</hostname>
    <SerialNumber>38xkf8</SerialNumber>
    <Uptime units="seconds">603835</Uptime>
  </device>
  <device>
    <hostname>name</hostname>
    <SerialNumber>495dkf</SerialNumber>
    <Uptime units="seconds">92548</Uptime>
  </device>
</devices>

I can parse through the info, but when I try and access the seconds, I cannot seem to get the data I want. When I try to access the Uptime element:

$xml.Devices.Device.Uptime

units                      #text
-----                      -----
seconds                    603835
seconds                    92548

I cannot get just the #text value returned.

Edit:

The solution:

Select-Xml -Xml $xml -XPath "//Device" | %
{
  $_.Node.Uptime.InnerText
}
MJ.
  • 139
  • 1
  • 2
  • 16

2 Answers2

1

Well formed XML needs equals sign before quotation marks

<Uptime units="seconds">603835</Uptime>
xeno
  • 11
  • 2
1

You can also use Select-Xml to do it:

Select-Xml -Xml $xml -XPath "//Uptime" | ForEach{$_.Node.InnerText}

Or more simply (may not work in older versions of PowerShell, I'm not sure):

(Select-Xml -Xml $xml -XPath "//Uptime").Node.InnerText
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56