fpmurphy
fpmurphy

Reputation: 2537

PowerShell CreateSignificantWhitespace

Consider the following PowerShell code snippet:

$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")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

According to current Microsoft documentation See https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.createsignificantwhitespace?view=net-5.0 for example, the following are valid whitespace "characters":

 
 
 and 	

What is the correct way to include a newline/linefeed in the argument string?

Upvotes: 1

Views: 61

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174795

PowerShell uses the backtick ` as an escape character:

$sigws1 = $xmldoc.CreateSignificantWhitespace("`n")

See the conceptual about_Special_Characters help topic.

Upvotes: 3

Related Questions