Rogue5099 Posted September 21, 2010 Share Posted September 21, 2010 I want to change <Value> from true to false in a .xml document but I can't get it to save?? <?xml version="1.0" encoding="utf-8"?> <Settings> <Section1> <Section2> <Value>true</Value> </Section2> </Section1> </Settings> I can read it and replace string but it won't save? If there is an easier way please let me know! My projects: Inventory / Mp3 Inventory, Computer Stats Link to comment Share on other sites More sharing options...
Varian Posted September 21, 2010 Share Posted September 21, 2010 (edited) Are you writing to the same file? Without any code I would say that it is a permission issue or handle (locked file) issue. Are you using FileOpen/FileClose? If so, are you using them correctly? Post the relevant code. Edited September 21, 2010 by Varian Link to comment Share on other sites More sharing options...
Rogue5099 Posted September 21, 2010 Author Share Posted September 21, 2010 (edited) This is a sample from the code. Once done it will handle serveral lines hince the complexity. #include <File.au3> #include <Array.au3> Global $LINES $sXmlFile = "C:\Test.xml" FileOpen($sXmlFile, 0) If _FILEREADTOARRAY($sXmlFile, $LINES) = 1 Then For $I = 1 To $LINES[0] If $LINES[$I] <> "" Then If StringInStr($LINES[$I], "<Value>true</Value>", 0) <> 0 Then StringReplace($LINES[$I], "true", "false") EndIf EndIf Next EndIf FileClose($sXmlFile) Not exactly what I wanted but this works: #include <File.au3> #include <Array.au3> Global $LINES $sXmlFile = "C:\Test.xml" If _FILEREADTOARRAY($sXmlFile, $LINES) = 1 Then For $I = 1 To $LINES[0] If $LINES[$I] <> "" Then If StringInStr($LINES[$I], "<Value>true</Value>", 0) <> 0 Then _FileWriteToLine($sXmlFile, $I, "<Value>false</Value>", 1) EndIf EndIf Next EndIf Edited September 21, 2010 by rogue5099 My projects: Inventory / Mp3 Inventory, Computer Stats Link to comment Share on other sites More sharing options...
PsaltyDS Posted September 21, 2010 Share Posted September 21, 2010 (edited) Learn XPath syntax and read/write to it directly: #include <_XMLDOMWrapper.au3> #include <Array.au3> ; Only for _ArrayDisplay() $sXmlFile = @ScriptDir & "\Test1.xml" $sXPath = "/Settings/Section1/Section2/Value" _XMLFileOpen($sXmlFile) For $n = 1 To 4 $aRET = _XMLGetValue($sXPath) _ArrayDisplay($aRET, "Before") $sNewVal = String($aRET[1] = "False") _XMLUpdateField($sXPath, $sNewVal) $aRET = _XMLGetValue($sXPath) _ArrayDisplay($aRET, "After") Next Edited September 21, 2010 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
wakillon Posted September 21, 2010 Share Posted September 21, 2010 Have you try with _ReplaceStringInFile () function ? AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
PsaltyDS Posted September 21, 2010 Share Posted September 21, 2010 Both StringReplace() and _ReplaceStringInFile() are going to become unwieldy and dangerous as the XML involved gets more complicated. What if you only wanted to change that value on the third of twenty selections in the file? Then change only the 12th one? Very easy to do with XPath, a real headache trying to parse the whole file with your own string manipulation. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now