private void SaveTreeView(XmlWriter writer) { if (writer == null) { return; } ItemCollection treeNodes = treeView.Items; if (treeNodes == null || treeNodes.Count == 0) { return; } SvgTestResult testResult = new SvgTestResult(); writer.WriteStartDocument(); writer.WriteStartElement("tests"); for (int i = 0; i < treeNodes.Count; i++) { TreeViewItem categoryItem = treeNodes[i] as TreeViewItem; if (categoryItem != null) { BulletDecorator header = categoryItem.Header as BulletDecorator; if (header == null) { continue; } TextBlock headerText = header.Child as TextBlock; if (headerText == null) { continue; } SvgTestCategory testCategory = new SvgTestCategory(headerText.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(); treeView.BeginInit(); treeView.Items.Clear(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && string.Equals(reader.Name, "category", StringComparison.OrdinalIgnoreCase)) { // <category label="Animations"> string category = reader.GetAttribute("label"); if (!string.IsNullOrWhiteSpace(category)) { SvgTestCategory testCategory = new SvgTestCategory(category); TextBlock headerText = new TextBlock(); headerText.Text = category; headerText.Margin = new Thickness(3, 0, 0, 0); BulletDecorator decorator = new BulletDecorator(); if (_folderClose != null) { Image image = new Image(); image.Source = _folderClose; decorator.Bullet = image; } else { Ellipse bullet = new Ellipse(); bullet.Height = 16; bullet.Width = 16; bullet.Fill = Brushes.Goldenrod; bullet.Stroke = Brushes.DarkGray; bullet.StrokeThickness = 1; decorator.Bullet = bullet; } decorator.Margin = new Thickness(0, 0, 10, 0); decorator.Child = headerText; TreeViewItem categoryItem = new TreeViewItem(); categoryItem.Header = decorator; categoryItem.Margin = new Thickness(0, 3, 0, 3); categoryItem.FontSize = 14; categoryItem.FontWeight = FontWeights.Bold; treeView.Items.Add(categoryItem); LoadTreeViewCategory(reader, categoryItem, testCategory); 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 = IoPath.ChangeExtension(_testResultsPath, ".bak"); try { if (File.Exists(backupFile)) { File.Delete(backupFile); } File.Move(_testResultsPath, backupFile); } catch (Exception ex) { MessageBox.Show(ex.ToString(), AppErrorTitle, MessageBoxButton.OK, MessageBoxImage.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(), AppErrorTitle, MessageBoxButton.OK, MessageBoxImage.Error); } } } treeView.EndInit(); }