如何解决PowerShell CreateSignificantWhitespace
考虑以下 PowerShell 代码片段:
$xmldoc = New-Object xml
$xmldoc.PreserveWhitespace = $true
# no error
$sigws1 = $xmldoc.CreateSignificantWhitespace(" ")
# error
$sigws2 = $xmldoc.CreateSignificantWhitespace("\n")
Exception calling "CreateSignificantWhitespace" with "1" argument(s): "The string for white space contains an invalid character."
At line:1 char:1
+ $sigws2 = $xml.CreateSignificantWhitespace("\n")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
根据当前的 Microsoft 文档,请参见 https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.createsignificantwhitespace?view=net-5.0 例如,以下是有效的空白“字符”:
 and 	
在参数字符串中包含换行符/换行符的正确方法是什么?
解决方法
PowerShell 使用反引号 `
作为转义字符:
$sigws1 = $xmldoc.CreateSignificantWhitespace("`n")
请参阅概念性的 about_Special_Characters 帮助主题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。