gpt4 book ai didi

delphi - 在 inno 脚本中使用 Xpath 读取 xml 文件

转载 作者:行者123 更新时间:2023-12-03 15:29:11 25 4
gpt4 key购买 nike

我有这个 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);

最佳答案

调用函数时,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');

关于delphi - 在 inno 脚本中使用 Xpath 读取 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11687661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com