I want to print the angular brackets(<,>) in powershell, but it is printing to > and < instead respectively. Is there any way I can print those symbols.
I have used \ to overcome the bracket's escape properties and also single inverted commas but the result remains same.
$xml = [xml](Get-Content -Path C:\Users\Desktop\new.xml)
$xml.Deploy.app="$<abc\bc>"
$xml.Save("C:\Users\Desktop\new.xml")
I expected output to be
<?xml version="1.0"?>
<Deploy>
<app><abc\bc></app>
</Deploy>
but it printing
<?xml version="1.0"?>
<Deploy>
<app><abc\bc></app>
</Deploy>
Thanks in advance!!
XML needs 5 characters to be replaced, because they have special meaning in XML. These characters are
<>&'"
Here, you can not simply prepend these characters with some escape character like the backslash, instead they need to be converted into named or numbered entities.As you have noticed, this conversion happens automatically in
[xml]
, but there are also a number of ways to do that manually if needed:1) Using the String
.Replace()
method:2) Using .Net System.Security:
3) Using System.Xml.XmlDocument:
4) Using Regex:
All of the above will convert the XML special characters into entities, so the result will look like this:
If you absolutely don't want the XML special characters to be converted into entities, then there is AFAIK only the option to set the value as a CDATA section, which will wrap your string inside
<![CDATA[
and]]>
]. To use that in your example, change the line$xml.Deploy.app="$<abc\bc>"
into this:Now it will print:
To read back this CDATA value as string, you can do either
$value = $xml.Deploy.app.InnerText
or$value = $xml.Deploy.app.'#cdata-section'
Hope that explains