Wednesday, October 8, 2008

[ConfigurationErrorsException]: '.', hexadecimal value 0x00, is an invalid character. Line 2, position 1.

I got this error after I used powershell script to change debug="false" to debug="true" for all web.config.

I found nothing wrong by comparing the deployed web.config and the web.config in the source tree.

It turned out an encoding issue after I re-saved the web.config using UTF-8 in notepad.

I modified the string replacing script to control the encoding.

$pattern ='debug="false"'
$replacement ='debug="true"'
$encode="default" # can be utf8 unicode default (it is actually ANSI)
$targetFile="web.config"

foreach ($file in (gci . -name $targetFile -rec)) {

if($file -match "PortalUI")
{
continue;
}
write-host process $file ...
write-host resave file using $encode
$text = get-content $file
$text | out-file $file -encoding $encode

$text = get-content $file

if ($text -match $pattern) {
write-host modifying $file
$text -replace $pattern, $replacement | out-file $file -encoding $encode
}
}

No comments: