2013-03-30 1 views
2

Мне нужна помощь с установкой inno, мне нужно сохранить некоторые узлы xml в определенной строке, но я не знаю, как это сделать.Inno Setup - Как сохранить узел в определенной строке

Это мой код

procedure SaveValueToXML(const AFileName, APath, AValue: string); 
var 
    XMLNode: Variant; 
    XMLDocument: Variant; 
begin 
    XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0'); 
    try 
    XMLDocument.async := False; 
    XMLDocument.load(AFileName); 
// if (XMLDocument.parseError.errorCode <> 0) then 
//  MsgBox('Install the software. ' + 
//  XMLDocument.parseError.reason, mbError, MB_OK) 
// else 
    begin 
     XMLDocument.setProperty('SelectionLanguage', 'XPath'); 
     XMLNode := XMLDocument.selectSingleNode(APath); 
     XMLNode.text := AValue; 
     XMLDocument.save(AFileName); 
    end; 
    except 
    MsgBox('Install the software', mbError, MB_OK); 
    end; 
end; 

function NextButtonClick(PageID: Integer): Boolean; 
var 
    XMLFile: string; 
begin 
    Result := True; 
    if (PageId = wpFinished) then 
    begin 
    XMLFile := ExpandConstant('{pf}\Hell\0\Config.xml'); 
    if FileExists(XMLFile) then 
    begin 
     SaveValueToXML(XMLFile, '//@param', PEdit.Text); //PEdit.text is from a custom input text box in the installer, ignore. 
     SaveValueToXML(XMLFile, '//@path', 
     ExpandConstant('{reg:HKCU\SOFTWARE\Craps,InstallPath}\Test.exe')); 
    end; 
    end; 
end; 

Это мой XML-файл:

<?xml version="1.0" encoding="UTF-8"?> 
<stuffs> 
     <stuff ident="555" path="C:\Program Files (x86)\Other thing\Other.exe" param="-alive" display="1" priority="0"/> 
     <stuff ident="666" path="C:\Program Files (x86)\Craps\test.exe" param="-dead" display="1" priority="0"/>  
</stuffs> 

Проблема заключается в том, что мой сценарий всегда пишет в первой строке. Мне нужно всегда сохранять узлы в строке, начинающейся с <stuff ident="666"

Заранее спасибо!

ответ

4

Вам необходимо использовать метод setAttribute, установленный в настройке text. Вот процедура для изменения значений атрибутов узла:

procedure SaveAttributeValueToXML(const AFileName, APath, AAttribute, 
    AValue: string); 
var 
    XMLNode: Variant; 
    XMLDocument: Variant; 
begin 
    XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0'); 
    try 
    XMLDocument.async := False; 
    XMLDocument.load(AFileName); 
    if (XMLDocument.parseError.errorCode <> 0) then 
     MsgBox('The XML file could not be parsed. ' + 
     XMLDocument.parseError.reason, mbError, MB_OK) 
    else 
    begin 
     XMLDocument.setProperty('SelectionLanguage', 'XPath'); 
     XMLNode := XMLDocument.selectSingleNode(APath); 
     XMLNode.setAttribute(AAttribute, AValue); 
     XMLDocument.save(AFileName); 
    end; 
    except 
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, 
     mbError, MB_OK); 
    end; 
end; 

А вот как запросить узел которого ident значение параметра 666 и чье param значение атрибута будет изменено на -alive:

SaveAttributeValueToXML('d:\File.xml', '//stuffs/stuff[@ident=''666'']', 
    'param', '-alive'); 

Для дополнительная информация о запросе XPath, используемом здесь, например, до this article.

+0

Спасибо за ответ! – Dielo

+1

Добро пожаловать! – TLama

Смежные вопросы