Adding a node to an existing XML file using inno setup

本小妞迷上赌 提交于 2020-01-25 10:52:06

问题


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


回答1:


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 }



回答2:


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
    



回答3:


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


来源:https://stackoverflow.com/questions/8194209/adding-a-node-to-an-existing-xml-file-using-inno-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!