private void addSectionToolStripMenuItem_Click(object sender, EventArgs e) { string newSectionName = sectionNameTextBox.Text; if (string.IsNullOrWhiteSpace(newSectionName)) { return; } TreeNode parentNode = sectionsTreeView.Nodes[0]; // root TreeNode selectedNode = sectionsTreeView.SelectedNode; if (selectedNode != null) { parentNode = selectedNode; } SectionBase parentSection = (SectionBase)parentNode.Tag; if (parentSection.Keys.Contains(newSectionName)) { return; } Sections newSection = new Sections(); parentSection[newSectionName] = newSection; TreeNode newNode = parentNode.Nodes.Add(newSectionName); newNode.Name = newSectionName; newNode.Tag = newSection; sectionsTreeView.Focus(); sectionsTreeView.SelectedNode = newNode; }
// TODO: refactor common code similarly to SerializeSectionBase() private static Settings DeserializeSettings(XElement xSettings) { Settings settings = new Settings(); if (xSettings == null) { return settings; } foreach (var node in xSettings.XPathSelectElements("child::*")) { XElement xElement = (XElement)node; if (xElement.Name == "section") { Sections section = new Sections(); settings[xElement.Attribute(XName.Get("name")).Value] = section; DeserializeSection(section, xElement); } else { settings.Parameters[xElement.Attribute(XName.Get("name")).Value] = xElement.Value; } } return settings; }
private static XElement SerializeSection(Sections section, string name) { XElement xSection = new XElement("section"); xSection.SetAttributeValue("name", name); SerializeSectionBase(section, xSection); return xSection; }
private static void DeserializeSection(Sections section, XElement xElement) { // TODO: looks like the code is almost the same as in DeserializeSettings() foreach (var node in xElement.XPathSelectElements("child::*")) { XElement nextEle = (XElement)node; if (nextEle.Name == "section") { Sections newSection = new Sections(); section[nextEle.Attribute(XName.Get("name")).Value] = newSection; DeserializeSection(newSection, nextEle); } else { section.Parameters[nextEle.Attribute(XName.Get("name")).Value] = nextEle.Value; } } }