4

我有这个 inno 脚本,我试图在其中读取 xml 文件并通过为用户提供更改为自定义输入的可能性来更改值。

这是我正在使用的代码:

function LoadValueFromXML(const AFileName, APath: string): string;
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  Result := '';
  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);
      Result := XMLNode.text;
    end;
  except
    MsgBox('An error occured!', mbError, MB_OK);
  end;
end;

这是调用上述函数以从 XML 加载值的代码

 EditMailServerID.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/Mail/MailServer/');
   EditMailServerUserId.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/Mail/UserName/');
   EditMailServerPassword.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/Mail/Password/');
   EditERPBOServicePath.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/default/ERPBOWebServicePath/');
   EditERPHOServicePath.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/default/ERPHOWebServicePath/');
   EditCustomerId.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/license/customernumber/');

示例 xml 代码是:

<?xml version="1.0" encoding="utf-8"?>
<settings> 
  <default>
    <ReadInvoiceFile>0</ReadInvoiceFile>
    <HOUser>
    </HOUser>
    <HOShopNo>1</HOShopNo>
    <LagerShop>500</LagerShop>
    <SenderAddress>administrator@datanova.no</SenderAddress>
    <HostId>192.168.6.155</HostId>
    <PincodeDownloadFileName>tilbud5.txt</PincodeDownloadFileName>
    <PrinterName />
    <UseAverageCostPrice>true</UseAverageCostPrice>
    <ERPBOWebServicePath>http://localhost:90/FlisekompanietERPBOWebService/ERPBOService.asmx</ERPBOWebServicePath>
    <ERPHOWebServicePath>http://localhost:90/FlisekompanietERPHOWebService/ERPHOService.asmx</ERPHOWebServicePath>   
  </default>
  <license>
    <customernumber>998877</customernumber>
    <custid>532</custid>
  </license> 
  <Mail>
    <MailServer>dnserver.datanova.no</MailServer>
    <UserName>sharma</UserName>
    <Password>datanova123</Password>
  </Mail>
  <sms>
    <provider>test</provider>
    <sendername>test</sendername>
    <username>test</username>
    <password>test</password>
  </sms> 
</settings>

我在以下位置收到运行时错误:

XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNode := XMLDocument.selectSingleNode(APath);
4

1 回答 1

4

/调用函数时,您的 XPath 表达式末尾有一个额外的斜杠。只需删除它们,您就可以使用它。这是修复:

EditMailServerID.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', 
  '//settings/Mail/MailServer');
EditMailServerUserId.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/Mail/UserName');
EditMailServerPassword.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/Mail/Password');
EditERPBOServicePath.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/default/ERPBOWebServicePath');
EditERPHOServicePath.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/default/ERPHOWebServicePath');
EditCustomerId.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/license/customernumber');
于 2012-07-27T12:35:33.317 回答