我正在尝试编写一个简短的安装脚本来更新 XML 文件 (myplatforinfo.config) 中的值。
我可耻地从 [this post on a similar task]{http://stackoverflow.com/questions/4129633/how-to-update-attributes-in-an-xml-file-with-installscript} 中窃取了代码,但是有为我的需要破解了它。
/// <summary>
/// This function reads in the fddId.config and alters the number associated
/// with the serial number fo the installed package.
/// <summary>
function UpdateDeviceFirmwareVersion(hMSI)
OBJECT oDoc;
STRING sConfigFilePath;
BOOL successfulLoad;
NUMBER retVal;
begin
sConfigFilePath = "C:\\myplatforinfo.config";
retVal = 0;
if (Is(FILE_EXISTS, sConfigFilePath)) = FALSE then
MessageBox("Could not find fddId file.", 0);
retVal = -1;
endif;
// get values from public properties
set oDoc = CreateObject("Msxml2.DOMDocument.4.0");
if (!IsObject(oDoc)) then
MessageBox("Could not create XML Document", 0);
retVal = -1;
endif;
oDoc.async = FALSE;
oDoc.setProperty("SelectionLanguage", "XPath");
successfulLoad = oDoc.load(sConfigFilePath);
if (successfulLoad < 0) then
MessageBox("Could not load the fddId as an xml file", SEVERE);
retVal = -1;
endif;
if (retVal = -1) then
return retVal;
abort;
endif;
ReplaceValueOf(oDoc, "//platformID/SerialNumber/version", "1");
oDoc.save(sConfigFilePath);
set oDoc = NOTHING;
end;
function ReplaceValueOf(oDoc, xPath, valueToPutIn)
OBJECT oNode;
begin
set oNode = oDoc.selectNodes(xPath)(0);
try
oNode.attributes.getNamedItem("value").value = valueToPutIn;
catch
MessageBox("Could not set '" + xPath + "' with '" + valueToPutIn + "'", SEVERE);
endcatch;
end;
然而,它在加载方法上失败了,需要帮助!:
successfulLoad = oDoc.load(sConfigFilePath);
if (successfulLoad < 0) then
MessageBox("Could not load the fddId as an xml file", SEVERE);
retVal = -1;
endif;
我不确定为什么。该文件是一个有效的 XML 文件,它可以由使用它的 .Net 代码处理。下面我包含了 XML 配置文件的简化版本。只是使用的标题和标签,但结构是相同的。它有注释,这会影响 InstallShield 解析器吗?
<?xml version="1.0" encoding="utf-8"?>
<platformIDxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<type>35</type>
<manufacturer>14</manufacturer>
<SerialNumber>
<version>2</version>
</SerialNumber>
</platformID>
我只与 InstallShield 2011 斗争了很短的时间,但我很快就学会了喜欢它的功能,讨厌它的开发支持和缺乏清晰的 UI。欢迎任何帮助:)。
编辑:正如 Michael Urman 所指出的那样,我没有正确处理来自我的 XML 文档加载的布尔返回。我已经进行了这个编辑,我的脚本现在已经到了“getNamedItem”并设置了值。
这可能是因为我希望设置的字段不是命名的“值”属性吗?我的元素没有任何属性,但我怀疑元素的值是一个名为“值”的属性,哈哈。我猜错了吗?
进一步编辑!!不,不是上面的……我已经尝试了 node.Value 和命名元素路由,调试器在新值的“setter”行上崩溃的两种方式。想我可能只需要使用内置功能。