0

I've got the following block of code:

[xml]$xml = @"
<response>
    <num>2</num>
    <time>7</time>
</response>
"@

$VARS = New-Object -TypeName PSObject `
                   -Property @{root = "response"
                               num  = "response.num"}

$xml.($VARS.root)  # test 1
$xml.($VARS.num)   # test 2

Test one returns the two elements num and time. Test two does not return anything (because there is no such element 'response.num' in the xml). Is it possible to get powershell to evaluate a String as a variable? So as above, if I have a string 'variable.property.sub-property', any way of getting powershell to return the value of sub-property?

I know I can do this with x-path with xml. But is there a syntactical way to get test 2 to work?

1 Answer 1

2

This seems to work:

iex "`$xml.$($vars.num)"   # test 2
2
Sign up to request clarification or add additional context in comments.

Comments

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.