Hi, I have a problem editing an xml file using ole automation. The first node addition works, on the second or third call I end up with error 0xC0000005 (setup exit code).
Here is my sample:
function WriteXmlAttrib(const filename, xmltag, atribute, value: string): boolean;
var
XMLDocW, editNodeW, editNodeW2, parentNodeW: Variant;
s, s1, s2, s3: string;
begin
Result := false;
try
XMLDocW := CreateOleObject('MSXML.DOMDocument'); //CreateOleObject('Msxml2.DOMDocument.6.0'); //same result
XMLDocW.async := False;
XMLDocW.resolveExternals := False;
XMLDocW.load(filename);
if XMLDocW.parseError.errorCode <> 0 then
//RaiseException('Error on line ' + IntToStr(XMLDocW.parseError.line) + ', position ' + IntToStr(XMLDocW.parseError.linepos) + ': ' + XMLDocW.parseError.reason);
Exit;
try
try
//XMLDocW.setProperty('SelectionLanguage', 'XPath');
except
end;
editNodeW := XMLDocW.selectSingleNode(xmltag);
s2 := editNodeW.Text;
editNodeW.setAttribute(atribute, value);
except
s := xmltag;
s3 := s;
editNodeW := null;
parentNodeW := XMLDocW.documentElement;
while pos('/', s) > 0 do
begin
if s1 = '' then
s1 := copy(s, 1, pos('/', s) - 1)
else
s1 := s1 + '/' + copy(s, 1, pos('/', s) - 1);
editNodeW := XMLDocW.selectSingleNode(s1);
try
s2 := editNodeW.Text;
except
s2 := s;
s2 := copy(s2, 1, pos('/', s2) - 1);
editNodeW := null;
editNodeW := XMLDocW.createElement(s2);
parentNodeW.appendChild(editNodeW);
end;
parentNodeW := editNodeW;
editNodeW := null;
s3 := s1;
s := copy(s, pos('/', s) + 1, 200);
end;
parentNodeW := null;
parentNodeW := XMLDocW.selectSingleNode(s3);
s1 := s;
if pos('[@', s) > 0 then
s1 := copy(s1, 1, pos('[@', s1) - 1);
editNodeW2 := null;
editNodeW2 := XMLDocW.createElement(s1);
if pos('[@', s) > 0 then
begin
s := copy(s, pos('[@', s) + 2, 200);
s1 := copy(s, 1, pos('=', s) - 1);
s := copy(s, pos('''', s) + 1, 200);
s := copy(s, 1, pos('''', s) - 1);
editNodeW2.setAttribute(s1, s);
end;
editNodeW2.setAttribute(atribute, value);
parentNodeW.appendChild(editNodeW2);
end;
XMLDocW.Save(filename);
Result := True;
finally
end;
end;
and theirs calling:
//allways ok
WriteXmlAttrib(ExpandConstant('{tmp}\abc.xml'), 'configuration/appSettings/add[@key=''Key3'']', 'value', 'key 3 value');
//sometime ok, sometime crash
WriteXmlAttrib(ExpandConstant('{tmp}\abc.xml'), 'configuration/appSettings/add[@key=''Key4'']', 'value', 'key 4 value');
//allways crash
WriteXmlAttrib(ExpandConstant('{tmp}\abc.xml'), 'configuration/appSettings/add[@key=''Key5'']', 'value', 'key 5 value');
I found the similar problems here
https://stackoverflow.com/questions/26034815/inno-setup-error-255-while-writing-xml-files-with-msxml-6 or here
https://stackoverflow.com/questions/16482070/adding-nodes-to-xml-with-dom-in-inno-setup-strange-problems.
Is there any way to solve this other than using an external dll or an application (with the same code of method WriteXmlAttrib) that handles the write?
Thanks for sugestions
PS