private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == string.Empty || textBox1.Text == "") { MessageBox.Show("名称不能为空", "错误"); return; } foreach (CSubProgram nSub in frmRecipe.ListSubProgram) { if (nSub.Name == textBox1.Text) { MessageBox.Show("名称 " + textBox1.Text + " 已经存在,请重新输入", "错误"); return; } } if (MessageBox.Show("是否保存子程序?", "保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No) { return; } CSubProgram newSub = new CSubProgram(); newSub.Name = textBox1.Text; newSub.Desc = textBox2.Text; newSub.sLayerList = textBox3.Text; frmRecipe.ListSubProgram.Add(newSub); string filePath = frmRecipe.sAppPath + @"\Project\Layer.xml"; XmlDocument myxmldoc = new XmlDocument(); myxmldoc.Load(filePath); string xpath = "root/SubProgramList"; XmlElement ListNode = (XmlElement)myxmldoc.SelectSingleNode(xpath); XmlElement nLayNode = myxmldoc.CreateElement("SubProgram"); // 创建根节点album nLayNode.SetAttribute("Name", textBox1.Text); nLayNode.SetAttribute("Desc", textBox2.Text); nLayNode.SetAttribute("sLayerList", textBox3.Text); ListNode.AppendChild(nLayNode); myxmldoc.Save(filePath); MessageBox.Show("保存成功", "成功"); this.Close(); }
private void SaveToXML() { if (MessageBox.Show("是否保存子程序列表?", "保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No) { return; } frmRecipe.ListSubProgram.Clear(); foreach (DataGridViewRow row in dataGridView1.Rows) { CSubProgram newSub = new CSubProgram(); newSub.Name = row.Cells["名称"].Value.ToString(); newSub.Desc = row.Cells["描述"].Value.ToString(); newSub.sLayerList = row.Cells["层列表"].Value.ToString(); frmRecipe.ListSubProgram.Add(newSub); } string filePath = frmRecipe.sAppPath + @"\Project\Layer.xml"; XmlDocument myxmldoc = new XmlDocument(); myxmldoc.Load(filePath); string xpath = "root/SubProgramList"; XmlElement ListNode = (XmlElement)myxmldoc.SelectSingleNode(xpath); while (ListNode.ChildNodes.Count > 0) { ListNode.RemoveChild(ListNode.FirstChild); } foreach (CSubProgram newSub in frmRecipe.ListSubProgram) { XmlElement nLayNode = myxmldoc.CreateElement("SubProgram"); // 创建根节点album nLayNode.SetAttribute("Name", newSub.Name); nLayNode.SetAttribute("Desc", newSub.Desc); nLayNode.SetAttribute("sLayerList", newSub.sLayerList); ListNode.AppendChild(nLayNode); } myxmldoc.Save(filePath); MessageBox.Show("保存成功", "成功"); }