2011-11-19 225 views
0

在我的Inno Setup的脚本将节点添加到现有的XML文件有一个[代码]部分,我需要添加一些代码:使用Inno Setup的

  1. 打开XML文件
  2. 再加入在一个特定的地方
  3. 将文件保存回硬盘

我需要能够编辑一个名为config.xml文件中\文档文件中的单个节点\ docotype

在文件中有一些像这样的代码:

<References> 
    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <string>System.dll</string> 
    <string>System.Core.dll</string> 
    <string>System.Drawing.dll</string> 
    <string>System.Windows.Forms.dll</string> 
    <string>System.XML.dll</string> 
    </ArrayOfString> 
</References> 

我需要它看起来像这样:

<References> 
    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <string>System.dll</string> 
    <string>System.Core.dll</string> 
    <string>System.Drawing.dll</string> 
    <string>System.Windows.Forms.dll</string> 
    <string>System.XML.dll</string> 
    <string>C:\\bin\Custom\cutty109.dll</string> 
    </ArrayOfString> 
</References> 

所以我真的只需要下面一行添加到该文件中的“ ArrayOfString”部分

<string>C:\\bin\Custom\cutty109.dll</string> 

我敢肯定,这一定是可能的,但我不知道如何..

感谢

+0

既然您确切知道文件最终会是什么样子,为什么不直接提供带有安装包的文件并让安装程序覆盖目标文件。 –

+1

与[此问题]几乎相同的问题和答案(http://stackoverflow.com/questions/8141886/inno-setup-modify-xml-file-based-on-custom-input) – Deanna

+1

这是为什么标记为各种VB标签呢? – Deanna

回答

0

尝试是这样的:

Dim sXPath : sXPath = "/configuration/References/ArrayOfString" 
    Dim sAdd : sAdd  = "C:\\bin\Custom\cutty109.dll" 
    Dim sElm : sElm  = "string" 
    Dim sFSpec : sFSpec = resolvePath("..\data\config.xml") 
    Dim oXDoc : Set oXDoc = CreateObject("Msxml2.DOMDocument") 
    oXDoc.setProperty "SelectionLanguage", "XPath" 
    oXDoc.async = False 
    oXDoc.load sFSpec 

    If 0 = oXDoc.ParseError Then 
    WScript.Echo sFSpec, "looks ok" 
    Dim ndFnd : Set ndFnd = oXDoc.selectSingleNode(sXPath) 
    If ndFnd Is Nothing Then 
     WScript.Echo "|", sXPath, "| not found" 
    Else 
     WScript.Echo "found |" & ndFnd.tagName & "|" 
     Dim ndNew : Set ndNew = oXDoc.createElement(sElm) 
     ndNew.appendChild oXDoc.createTextNode(sAdd) 
     ndFnd.appendChild ndNew 
     WScript.Echo "After appending:" 
     WScript.Echo oXDoc.xml 
     oXDoc.Save Replace(sFSpec, ".xml", "-2.xml") 
    End If 
    Else 
    WScript.Echo oXDoc.ParseError.Reason 
    End If 

步骤:

  • 创建Msxml2.DOMDocument
  • 使用XPath来查找节点改变
  • 创建一个字符串,现在元素并附加文本
  • 将新节点追加到找到的节点
  • 保存修改后的XML
+0

良好的开端,但InnoSetup使用PascalScript –

1

我假设你真的需要一些动态的方式添加到该配置文件,如果没有的话当然覆盖旧的是最简单的方法。

要动态部分添加到配置文件中,你有一些选择:

  1. 您可以创建自己的命令行实用程序(EXE或脚本),做文件操作和调用实用程序安装脚本的[Run]部分。这可能是这个样子:

    • [Files]部分,你必须为你的效用一行:

      来源:“myUtil.exe”; DESTDIR:“{}应用”

    • [Run]部分,你必须为每个你需要在你的配置做的,这样操作一个行:

      文件名:“{应用} \ myUtil。exe文件“;参数 ”/ Addsection的:“

    OR

  2. 您可以使用帕斯卡尔脚本来操纵你的配置文件,您可以创建一个使用CreateOleObject调用msxml.dll XML的一个Pascal。文件操作然后,在你[Files]部分,您可以使用AfterInstall来打电话给你的Pascal函数,像这样:

    Source: "myFileThatNeedsConfigManipulation.dll"; DestDir: ... ; 
        AfterInstall: MyPascalFunctionThatDoesTheManipulation 
    
3

请参考CodeAutomation.iss example随inno安装一起提供。并使用此代码代替“修改XML文档”部分下的原始代码。

{ Modify the XML document } 

    NewNode := XMLDoc.createElement('string'); 
    XMLDoc.setProperty('SelectionLanguage', 'XPath'); 
    RootNode := XMLDoc.selectSingleNode('//References/ArrayOfString'); 
    RootNode.appendChild (NewNode); 
    RootNode.lastChild.text :='C:\\bin\Custom\cutty109.dll'; 

    { Save the XML document }