private void OpenFile(string filePath) { try { lstScriptActions.Items.Clear(); scriptVariables = new List <Core.Script.ScriptVariable>(); this.ScriptFilePath = filePath; Core.Script.Script deserializedScript = Core.Script.Script.DeserializeFile(ScriptFilePath); if (deserializedScript.Commands.Count == 0) { Notify("Error Parsing File: Commands not found!"); } scriptVariables = deserializedScript.Variables; PopulateExecutionCommands(deserializedScript.Commands); FormatCommandListView(); Notify("Script Loaded Successfully!"); } catch (Exception ex) { Notify("Oops, an error occured: " + ex.Message); } }
public static Script SerializeScript(string scriptFilePath, ListView.ListViewItemCollection scriptCommands, List <ScriptVariable> scriptVariables) { //create fileStream var fileStream = System.IO.File.Create(scriptFilePath); var script = new Core.Script.Script(); //save variables to file script.Variables = scriptVariables; //save listview tags to command list int lineNumber = 1; List <Core.Script.ScriptAction> subCommands = new List <Core.Script.ScriptAction>(); foreach (ListViewItem commandItem in scriptCommands) { var command = (Core.AutomationCommands.ScriptCommand)commandItem.Tag; command.LineNumber = lineNumber; if ((command is Core.AutomationCommands.BeginLoopCommand) || (command is Core.AutomationCommands.BeginIfCommand)) { if (subCommands.Count == 0) //if this is the first loop { //add command to root node var newCommand = script.AddNewParentCommand(command); //add to tracking for additional commands subCommands.Add(newCommand); } else //we are already looping so add to sub item { //get reference to previous node var parentCommand = subCommands[subCommands.Count - 1]; //add as new node to previous node var nextNodeParent = parentCommand.AddAdditionalAction(command); //add to tracking for additional commands subCommands.Add(nextNodeParent); } } else if ((command is Core.AutomationCommands.EndLoopCommand) || (command is Core.AutomationCommands.EndIfCommand)) //if current loop scenario is ending { //get reference to previous node var parentCommand = subCommands[subCommands.Count - 1]; //add to end command // DECIDE WHETHER TO ADD WITHIN LOOP NODE OR PREVIOUS NODE parentCommand.AddAdditionalAction(command); //remove last command since loop is ending subCommands.RemoveAt(subCommands.Count - 1); } else if (subCommands.Count == 0) //add command as a root item { script.AddNewParentCommand(command); } else //we are within a loop so add to the latest tracked loop item { var parentCommand = subCommands[subCommands.Count - 1]; parentCommand.AddAdditionalAction(command); } //increment line number lineNumber++; } //output to xml file XmlSerializer serializer = new XmlSerializer(typeof(Script)); var settings = new XmlWriterSettings(); settings.NewLineHandling = NewLineHandling.Entitize; settings.Indent = true; //write to file using (XmlWriter writer = XmlWriter.Create(fileStream, settings)) { serializer.Serialize(writer, script); } fileStream.Close(); return(script); }