Text in XML-Datei ersetzen

  • Hallo, ich habe folgende Text in einer XML Datei

    [autoit]


    <Feature Id="SetupControllerFiles" Cost="1248">
    <OptionRef Id="AlwaysInstalled"/>
    </Feature>
    <Feature Id="SetupXmlFiles" Cost="1248">
    <OptionRef Id="AlwaysInstalled"/>
    </Feature>

    [/autoit]

    Nun würd ich gern
    <Feature Id="SetupXmlFiles" Cost="1248">
    <OptionRef Id="AlwaysInstalled"/>
    </Feature>

    mit
    <Feature Id="SetupXmlFiles" Cost="1248">
    <OptionRef Id="neverInstalled"/>
    </Feature>

    ersetzen.

    Folgendes Script Funtioniert leider nicht, ich vermute es liegt an der formatierung der zeile

    [autoit]


    $szFile= "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\Office Setup Controller\Proof.en\Proof.xml"
    $szText= FileRead($szFile,FileGetSize($szFile))
    $mytext='<Feature Id="SetupXmlFiles" Cost="1248"><OptionRef Id="AlwaysInstalled"/>'
    $mytextNew= '<Feature Id="SetupXmlFiles" Cost="1248"><OptionRef Id="neverInstalled"/>'
    $szText= StringReplace($szText,$myText,$myTextNew)

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    MsgBox(4096,"Test", "text: " & $sztext)

    [/autoit][autoit][/autoit][autoit]

    MsgBox(4096,"Test", "text: " & $mytext)
    MsgBox(4096,"Test", "new: " & $mytextnew)

    [/autoit]

    Einmal editiert, zuletzt von basti19855 (15. August 2011 um 15:27)

  • Du könntest statt fileread auch _filereadtoarray verwenden, dann dürftest du keine Probleme mit den Formatierungen bekommen. Hier ein ungetestetes Script:

    Spoiler anzeigen
    [autoit]


    #include <array.au3>
    #include <file.au3>

    [/autoit] [autoit][/autoit] [autoit]

    global $file = @scriptdir & "\test.xml"
    global $aFile ; array für den Dateiinhalt

    [/autoit] [autoit][/autoit] [autoit]

    _filereadtoarray($file,$afile)
    _arraydisplay($afile) ; debug
    $startIndex = _arraysearch($aFile,'<Feature Id="SetupXmlFiles" Cost="1248">',0,0,0,1)
    if $startIndex <> -1 then
    $aFile[$startindex+1]=stringreplace($aFile[$startindex+1],"AlwaysInstalled","neverInstalled")
    else
    msgbox(0,"fehler","Suchstring wurde nicht gefunden oder Array fehlerhaft... errorcode: " & @error)
    endif
    _arraydisplay($afile) ; debug
    ;_filewritefromArray($file,$afile,1) ; würde die alte Datei dann überschreiben....

    [/autoit]

    Einmal editiert, zuletzt von misterspeed (15. August 2011 um 16:12)