-1

Related to Inno setup: how to replace a string in XML file?

As suggested in the answer I'm using a template xml, let's say "app.xml". I would like to replace a string in that file: "Dsn=Serverxxx" with the result of a function "DsnName" {code:DsnName} I use elsewhere in the script.

[Code]
procedure WriteAppPath;
var
    FileData: String;
begin
    LoadStringFromFile(ExpandConstant('{app}\app.xml'), FileData);
    StringChange(FileData, 'XXXXXMARKERXXXXX', ExpandConstant('{app}'));
    SaveStringToFile(ExpandConstant('{app}\app.xml'), FileData, False);
end;

I cant seem to get this to work. How would the code above look acomplishing this? And, how would it look using StringChangeEx?

I know this will probably be marked as duplicate, but I just can't figure it out.

Thanks.

bRins
  • 83
  • 9
  • Possible duplicate of [LoadStringFromFile and StringChangeEx from Unicode Inno Setup (Ansi file)](https://stackoverflow.com/questions/20912510/loadstringfromfile-and-stringchangeex-from-unicode-inno-setup-ansi-file) – Andrew Truckle Mar 16 '18 at 23:52
  • *"I cant seem to get this to work"* is useless problem description. We need [mcve]. – Martin Prikryl Mar 19 '18 at 06:48

1 Answers1

1

I think if you look at the Inno Setup help it explains it:

http://www.jrsoftware.org/ishelp/index.php?topic=isxfunc_stringchangeex

Description:

Changes all occurrences in S of FromStr to ToStr. If SupportDBCS is True (recommended unless you require binary safety), double-byte character sequences in S are recognized and handled properly. Otherwise, the function behaves in a binary-safe manner. Returns the number of times FromStr was matched and changed.

So what you are doing is:

  1. Reading in your text file as one long text string.
  2. Changing all instances of Xxx with Yyy in the string.
  3. Saving the string back to the file.

But looking here it does states:

Loads the specified binary or non Unicode text file into the specified string. Returns True if successful, False otherwise.

So this function might not be ok for you because XML files are usually Unicode.

The answer here explains.

Community
  • 1
  • 1
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164