2

In my inno setup script there is a [code] section and I need to add some code to:

  1. Open an xml file
  2. then add a single node in a specific place
  3. Save the file back to the hard drive

I need to be able to edit a file called config.xml in \documents\docotype

in the file there is some code like this:

<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>

I need it to look like this:

<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>

So really I just need to add the following line into the file in the 'ArrayOfString' section

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

I'm sure this must be possible but I have no clue how..

Thanks

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
Fasani
  • 5,141
  • 1
  • 23
  • 24
  • Since you know exactly how the file will look like in the end, why don't you just supply the file with the installation package and let the setup overwrite the target. – Sertac Akyuz Nov 19 '11 at 14:17
  • 1
    Pretty much the same question and answer as [this question](http://stackoverflow.com/questions/8141886/inno-setup-modify-xml-file-based-on-custom-input) – Deanna Nov 21 '11 at 09:09
  • 1
    Why is this tagged with various VB tags too? – Deanna Nov 21 '11 at 09:10

3 Answers3

3

Please refer the CodeAutomation.iss example provided along with inno install. And use this code instead the original code under the 'Modify the XML document' section.

{ 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 }
Deanna
  • 23,876
  • 7
  • 71
  • 156
anand
  • 618
  • 1
  • 9
  • 26
1

I assume you really need some dynamic way to add to this config file, if not then of course overriding the old one is the simplest method.

To dynamically add sections to a config file, you have some options:

  1. You can create your own command line utility (exe or script) that does the file manipulation and call that utility in the [Run] section of your install script. This could look something like this:

    • In the [Files] section, you'll have one line for your utility:

      Source: "myUtil.exe"; DestDir: "{app}"

    • In the [Run] section, you'll have one line for each manipulation you need to do in your config, like this:

      FileName: "{app}\myUtil.exe"; Parameters: "/addSection:"

    OR

  2. You can use Pascal scripting to manipulate your config file. You can create a Pascal that uses CreateOleObject to call msxml.dll for XML the file manipulation. Then, in your [Files] section you can use AfterInstall to to call your Pascal function, like this:

    Source: "myFileThatNeedsConfigManipulation.dll"; DestDir: ... ; 
        AfterInstall:  MyPascalFunctionThatDoesTheManipulation
    
Otiel
  • 18,404
  • 16
  • 78
  • 126
GTG
  • 4,914
  • 2
  • 23
  • 27
0

Try something like this:

  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

The steps:

  • create a Msxml2.DOMDocument
  • use XPath to find the node to change
  • create a now string element and append the text
  • append the new node to the found node
  • save the modified XML
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96