我有这个inno脚本,我试图读取一个xml文件,并通过给用户更改为自定义输入的可能性来更改值. 这是我正在使用的代码: function LoadValueFromXML(const AFileName, APath: string): string;var XMLNode: Varia
这是我正在使用的代码:
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);调用函数时,在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');