我不能重現你的問題。需要更多信息。
的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