private void SaveTreeView(XmlWriter writer) { if (writer == null) { return; } TreeNodeCollection treeNodes = treeView.Nodes; if (treeNodes == null || treeNodes.Count == 0) { return; } SvgTestResult testResult = new SvgTestResult(); writer.WriteStartDocument(); writer.WriteStartElement("tests"); var selectedSuite = _optionSettings.SelectedTestSuite; if (selectedSuite != null) { writer.WriteAttributeString("version", selectedSuite.Version); writer.WriteAttributeString("description", selectedSuite.Description); } for (int i = 0; i < treeNodes.Count; i++) { TreeNode categoryItem = treeNodes[i] as TreeNode; if (categoryItem != null) { SvgTestCategory testCategory = new SvgTestCategory(categoryItem.Text); this.SaveTreeViewCategory(writer, categoryItem, testCategory); if (testCategory.IsValid) { testResult.Categories.Add(testCategory); } } } writer.WriteEndElement(); writer.WriteEndDocument(); if (_testResults == null) { _testResults = new List <SvgTestResult>(); } if (testResult.IsValid) { if (_testResults.Count == 0) { _testResults.Add(testResult); } } else { int foundAt = -1; for (int i = 0; i < _testResults.Count; i++) { SvgTestResult nextResult = _testResults[i]; if (nextResult != null && nextResult.IsValid) { if (string.Equals(nextResult.Version, testResult.Version)) { foundAt = i; break; } } } if (foundAt >= 0) { SvgTestResult nextResult = _testResults[foundAt]; if (!SvgTestResult.AreEqual(nextResult, testResult)) { _testResults[foundAt] = testResult; } } else { _testResults.Add(testResult); } } }
private void LoadTreeView(XmlReader reader) { SvgTestResult testResult = new SvgTestResult(); // Suppress repainting the TreeView until all the objects have been created. treeView.BeginUpdate(); // Clear the TreeView each time the method is called. treeView.Nodes.Clear(); string selectedCategory = string.Empty; string selectedTest = string.Empty; if (_optionSettings != null && !string.IsNullOrWhiteSpace(_optionSettings.SelectedValuePath)) { var selectedPaths = _optionSettings.SelectedValuePath.Split('/'); if (selectedPaths != null && selectedPaths.Length == 2) { selectedCategory = selectedPaths[0]; selectedTest = selectedPaths[1]; } } TreeNode selectedCategoryItem = null; var treeFont = this.treeView.Font; this.treeView.ItemHeight = 22; var categoryFont = new Font(treeFont.FontFamily, treeFont.Size + 1, FontStyle.Bold, GraphicsUnit.Pixel); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && string.Equals(reader.Name, "category", StringComparison.OrdinalIgnoreCase)) { // <category label="Animations"> string categoryLabel = reader.GetAttribute("label"); if (!string.IsNullOrWhiteSpace(categoryLabel)) { SvgTestCategory testCategory = new SvgTestCategory(categoryLabel); TreeNode categoryItem = new TreeNode(categoryLabel); categoryItem.NodeFont = categoryFont; categoryItem.ImageIndex = 0; categoryItem.SelectedImageIndex = 0; categoryItem.Tag = testCategory; treeView.Nodes.Add(categoryItem); bool categorySelected = false; if (!string.IsNullOrWhiteSpace(selectedCategory) && selectedCategory.Equals(categoryLabel, StringComparison.OrdinalIgnoreCase)) { selectedCategoryItem = categoryItem; categorySelected = true; } LoadTreeViewCategory(reader, categoryItem, testCategory, categorySelected, selectedTest); if (testCategory.IsValid) { testResult.Categories.Add(testCategory); } } } } if (_testResults == null) { _testResults = new List <SvgTestResult>(); } bool saveResults = false; if (testResult.IsValid) { if (_testResults.Count == 0) { _testResults.Add(testResult); saveResults = true; } else { int foundAt = -1; for (int i = 0; i < _testResults.Count; i++) { SvgTestResult nextResult = _testResults[i]; if (nextResult != null && nextResult.IsValid) { if (string.Equals(nextResult.Version, testResult.Version)) { foundAt = i; break; } } } if (foundAt >= 0) { SvgTestResult nextResult = _testResults[foundAt]; if (!SvgTestResult.AreEqual(nextResult, testResult)) { _testResults[foundAt] = testResult; saveResults = true; } } else { _testResults.Add(testResult); saveResults = true; } } } if (saveResults) { if (!string.IsNullOrWhiteSpace(_testResultsPath)) { string backupFile = null; if (File.Exists(_testResultsPath)) { backupFile = Path.ChangeExtension(_testResultsPath, ".bak"); try { if (File.Exists(backupFile)) { File.Delete(backupFile); } File.Move(_testResultsPath, backupFile); } catch (Exception ex) { if (!string.IsNullOrWhiteSpace(backupFile) && File.Exists(backupFile)) { File.Delete(backupFile); } MessageBox.Show(ex.ToString(), MainForm.AppErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } try { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = " "; settings.Encoding = Encoding.UTF8; using (XmlWriter writer = XmlWriter.Create(_testResultsPath, settings)) { this.SaveTestResults(writer); } } catch (Exception ex) { if (!string.IsNullOrWhiteSpace(backupFile) && File.Exists(backupFile)) { File.Move(backupFile, _testResultsPath); } MessageBox.Show(ex.ToString(), MainForm.AppErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } if (!string.IsNullOrWhiteSpace(backupFile) && File.Exists(backupFile)) { File.Delete(backupFile); } } } if (selectedCategoryItem != null) { selectedCategoryItem.ExpandAll(); } // Begin repainting the TreeView. treeView.EndUpdate(); treeView.Focus(); }