Get XML attribute values

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5137

    by slash85 at 2012-09-24 01:50:44

    Hi all,

    I'm trying to store the XML attribute values from this command:

    [xml]$userfile = get-content test.xml

    $NAME = $userfile | select-xml -xpath "//NAME//@SNAME"

    $NAME

    However this does not output the value only the node information (as below)

    Node Path Pattern
    —- —- ——-
    SNAME InputStream //NAME//@SNAME
    SNAME InputStream //NAME//@SNAME
    SNAME InputStream //NAME//@SNAME

    How can i retrieve the actual values in SNAME and store in a variable?

    Many Thanks for any help – i'm sure it's easy but i'm not having much luck.

    I've tried piping to | foreach {$_.value} but still nothing?

    Slash.

    by DonJ at 2012-09-24 06:34:39

    I'm not the awesome-est at XML manipulation, but in general you'll want to pipe whatever you've got to Get-Member. That'll show you what it's made of. Could you post a short excerpt of your XML? Not the whole thing, just a short version. It's hard to help you with this without knowing what the input looks like.

    by surveyor at 2012-09-24 07:28:54

    Hi slash,
    with a little modification from this thread it shows maybe what you need:
    Clear-Host

    $myxml = [xml]@"
    < ?xml version="1.0" encoding="utf-8" standalone="no" ?>

















    Testtext


    "@

    # get attributes
    $Field2 = Select-Xml -Xml $myxml -XPath "/fieldData/map/field[@name='Field 2']"
    $Field2.Node.area

    # get the value
    $Field3 = Select-Xml -Xml $myxml -XPath "/fieldData/map/field[@name='Field 3']"
    $Field3.Node.InnerText
    # or better
    $Field3.Node.InnerText.Trim()

You must be logged in to reply to this topic.