MJ.
MJ.

Reputation: 149

Access XML Node

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
}

Upvotes: 0

Views: 83

Answers (2)

TheMadTechnician
TheMadTechnician

Reputation: 36322

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

Upvotes: 1

xeno
xeno

Reputation: 11

Well formed XML needs equals sign before quotation marks

<Uptime units="seconds">603835</Uptime>

Upvotes: 1

Related Questions