private void saveButton_Click(object sender, EventArgs e) { DialogResult confirm = MessageBox.Show("Are you sure you want to save your changes? This will overwrite your current bookmarks file.", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirm == DialogResult.Yes) { TrimTree(); XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("bookmarks"); foreach (TreeNode n in stationTreeView.Nodes) { XmlElement group = doc.CreateElement("group"); group.SetAttribute("name", n.Text); group.SetAttribute("img", n.Tag.ToString()); foreach (TreeNode r in n.Nodes) { XmlElement bookmark = doc.CreateElement("bookmark"); bookmark.SetAttribute("name", r.Text); var metainfo = (StationInfo)r.Tag; bookmark.SetAttribute("url", metainfo.url); bookmark.SetAttribute("img", metainfo.img); group.AppendChild(bookmark); } root.AppendChild(group); } doc.AppendChild(root); doc.Save(path + "\\bookmarks.xml"); SysTrayApp.updateStations(); this.Close(); } }
private void saveChanges_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("bookmarks"); Boolean invalidURLs = false; foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.IsNewRow) { continue; } string tmpURL = row.Cells[2].FormattedValue.ToString(); int rowIndex = -1; DataGridViewRow tmpRow = dataGridView1.Rows .Cast <DataGridViewRow>() .Where(r => r.Cells[2].Value.ToString().Equals(tmpURL)) .First(); rowIndex = row.Index; if (!validateURL(tmpURL)) { invalidURLs = true; dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[2]; dataGridView1.CurrentCell.Style = new DataGridViewCellStyle { ForeColor = Color.Red }; } else { dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[2]; dataGridView1.CurrentCell.Style = new DataGridViewCellStyle { ForeColor = Color.Black }; } } foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.IsNewRow) { continue; } string tmpURL = row.Cells[2].FormattedValue.ToString(); int rowIndex = -1; DataGridViewRow tmpRow = dataGridView1.Rows .Cast <DataGridViewRow>() .Where(r => r.Cells[2].Value.ToString().Equals(tmpURL)) .First(); rowIndex = row.Index; if (!validateURL(tmpURL)) { dataGridView1.FirstDisplayedScrollingRowIndex = rowIndex; break; } } if (invalidURLs == true) { dataGridView1.ClearSelection(); MessageBox.Show("I found at least one invalid URL, hold up!", "Invalid URL!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var distinctGroups = dataGridView1.Rows.Cast <DataGridViewRow>() .Where(x => !x.IsNewRow) // either.. .Where(x => x.Cells[0].Value != null) //..or or both .Select(x => x.Cells[0].Value.ToString()) .Distinct() .ToList(); distinctGroups.ForEach(delegate(String groupName) { XmlElement group = doc.CreateElement("group"); group.SetAttribute("name", groupName); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[0].Value != null) { if (row.Cells[0].Value.ToString() == groupName) { XmlElement bookmark = doc.CreateElement("bookmark"); bookmark.SetAttribute("name", row.Cells[1].Value.ToString()); bookmark.SetAttribute("url", row.Cells[2].Value.ToString()); group.AppendChild(bookmark); } } } root.AppendChild(group); doc.AppendChild(root); }); Console.WriteLine(doc.OuterXml); DialogResult confirm = MessageBox.Show("Are you sure? This will permanently overwrite your existing bookmarks.", "Confirm Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirm == DialogResult.Yes) { doc.Save(path + "\\bookmarks.xml"); SysTrayApp.updateStations(); this.Close(); } }