Given the following xml:
<foo bar="&foobar">some text</foo>
I need to get the value of the bar attribute without it being unescaped. Every method I've tried thus far in PowerShell results in this value:
&foobar
Rather than this:
&foobar
I need the latter, as I need the literal, properly escaped value to persist.
If I do this:
[xml]$xml = "<foo bar='&foobar'>some text</foo>"
$xml.foo.bar
The attribute value is unescaped (i.e. &foobar).
If I do this:
$val = $xml | select-xml "/foo/@bar"
$val.Node.Value
The attribute value is unescaped (i.e. &foobar).
What is the best way to ensure that I get the original, escaped value of an attribute with PowerShell?
[Security.SecurityElement]::Escape($xml.foo.bar)
Using the sample XML above, each of the following will produce the original, escaped value for the bar attribute:
Using XPath:
$val = $xml | select-xml "/foo/@bar"
$val.Node.get_innerXml()
Using PowerShell's native XML syntax:
$xml.foo.attributes.item(0).get_innerXml()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With