我不能重现你的问题。需要更多信息。
的test.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<exampleOfACDATA>
<![CDATA[
This is Graimer`s [CDATA[]] test.
Notice that I just used a backtick in my text,
which is PowerShell's escape character.
I'm gonna store this in `$s` in PS.
]]>
</exampleOfACDATA>
Powershell的
PS > $xml = [xml](Get-Content .\Desktop\test.xml)
$s = $xml.exampleOfACDATA."#cdata-section"
$s
This is Graimer`s [CDATA[]] test.
Notice that I just used a backtick in my text,
which is PowerShell's escape character.
I'm gonna store this in `$s` in PS.
一般来说,以确保特殊字符表现得像普通的字符,变量不扩大等,用单引号'text'
你周围串。当您从其他来源(函数,文件...)获取字符串时,这是powershell的正常行为。
我没有看到问题。使用单引号引用字符串。这将保持特殊字符并避免扩展变量。只要你使用单引号,PowerShell不会逃脱任何事情。 'Read-Host'也会使用单引号创建字符串。如果你需要某种原因确实需要逃避escape-chars,你可以使用'$ text =(Read-Host“Test”) - 替换'backtick','2xbacktick'' – 2013-04-09 07:33:18
@Graimer,FYI,字符串是作为输入到XML CDATA部分提供的脚本中,并且PS脚本以可变内容获取它,而不是单引号。 – RinoTom 2013-04-09 17:24:49
我仍然无法重现它。请参阅“答案”,证明我正在经历什么。你能提供更多信息吗?示例xml,您的代码和您收到的输出。这将使这更容易:-) – 2013-04-09 17:59:48