Search code examples
asp.netcharacter-encodingweb-configinstallshieldbyte-order-mark

Garbage characters after update of web.config using c# on China (Traditional) machine


I am updating web.config file of Asp.net mvc dynamically while installation using installshiled script.

It works correctly on all machines; however it generates ??? charaters on Chinese machine at the start of web.config file like below.

???<?xml version="1.0" encoding="utf-8"?>

Please suggest how this problem can be please.

Below is the Installshield code

Using installscript I am finding connection string place holder and replacing that with connection string generated while installation.

szIniFile = INSTALLDIR^"AppDir\\Web.config";
szSearchStr = "[COONECTIONSTRING]";
FindAndReplaceInFile(szIniFile, szSearchStr,strWebConString);


function FindAndReplaceInFile(szFile, szSearchStr,szReplaceStr)
    STRING szReturnLine,szString, szSecPart,szFirstPart,svString,szArchive;
    NUMBER  nResult,nSubPos,nSearchStrLen,nLineNumber;   
    begin     
   nSearchStrLen = StrLength(szSearchStr); 
   nResult=FileGrep (szFile, szSearchStr, szReturnLine, nLineNumber, 
   RESTART) ;

   NumToStr ( svString, nResult ); 

   while (nResult=0)    
        nSubPos    = StrFind(szReturnLine, szSearchStr);    //get position of szSearchStr
        StrSub (szFirstPart, szReturnLine, 0, nSubPos);         
        StrSub (szSecPart, szReturnLine, nSubPos+nSearchStrLen, StrLength(szReturnLine));
        szString="";
        szString = szFirstPart+szReplaceStr+szSecPart;
        FileInsertLine (szFile, szString, nLineNumber, REPLACE);
        nLineNumber = nLineNumber + 1;
        nResult=FileGrep (szFile, szSearchStr, szReturnLine, nLineNumber,CONTINUE) ;

    endwhile; 
end;

Solution

  • You had a byte order marker (BOM) at the start of the file.

    I suspect that what has happened is you opened a UTF8 encoded file in as a different encoding. This misread the unnecessary BOM and corrupted it. When you saved it, the unknown character markers replaced the BOM.

    To rectify this, you need to encode your config as UTF8 without a BOM. Edits should then be safe, unless you have other characters outside the ASCII range in your file.