private void addUserB_Click(object sender, EventArgs e) { AddUser adu = new AddUser(); DialogResult dr = adu.ShowDialog(); if (dr == DialogResult.OK) { XmlDocument xDoc = new XmlDocument(); xDoc.Load("data.xml"); XmlElement xRoot = xDoc.DocumentElement; foreach (XmlNode xnodeGroups in xRoot) { XmlNode attr = xnodeGroups.Attributes.GetNamedItem("id"); if (attr.Value == groups[groupsList.SelectedIndex].id.ToString()) { // создаем новый элемент user XmlElement userElem = xDoc.CreateElement("user"); // создаем атрибуты XmlAttribute idAttr = xDoc.CreateAttribute("id"); XmlAttribute surnameAttr = xDoc.CreateAttribute("surname"); XmlAttribute nameAttr = xDoc.CreateAttribute("name"); XmlAttribute patronAttr = xDoc.CreateAttribute("patron"); XmlAttribute birthdayAttr = xDoc.CreateAttribute("birthday"); XmlText surnameText = xDoc.CreateTextNode(adu.surnameBox.Text); XmlText nameText = xDoc.CreateTextNode(adu.nameBox.Text); XmlText patronText = xDoc.CreateTextNode(adu.patronBox.Text); XmlText birthdayText = xDoc.CreateTextNode(adu.dateTimePicker1.Value.ToShortDateString()); int id = 0; foreach (User us in group) { if (us.id > id) { id = us.id; } } XmlText idText = xDoc.CreateTextNode(Convert.ToString(id + 1)); idAttr.AppendChild(idText); surnameAttr.AppendChild(surnameText); nameAttr.AppendChild(nameText); patronAttr.AppendChild(patronText); birthdayAttr.AppendChild(birthdayText); userElem.Attributes.Append(idAttr); userElem.Attributes.Append(nameAttr); userElem.Attributes.Append(surnameAttr); userElem.Attributes.Append(patronAttr); userElem.Attributes.Append(birthdayAttr); xnodeGroups.AppendChild(userElem); xDoc.Save("data.xml"); break; } } UpdateUsersList(); } }
private void editUserB_Click(object sender, EventArgs e) { if (usersList.SelectedIndex != -1) { int ind = usersList.SelectedIndex; AddUser adu = new AddUser(group[ind].name, group[ind].surname, group[ind].patron, group[ind].birthday); DialogResult dr = adu.ShowDialog(); if (dr == DialogResult.OK) { XmlDocument xDoc = new XmlDocument(); xDoc.Load("data.xml"); XmlElement xRoot = xDoc.DocumentElement; foreach (XmlNode xnodeGroups in xRoot) { XmlNode attr = xnodeGroups.Attributes.GetNamedItem("id"); if (attr.Value == groups[groupsList.SelectedIndex].id.ToString()) { foreach (XmlNode xnode in xnodeGroups) { XmlNode attr2 = xnode.Attributes.GetNamedItem("id"); if (attr2.Value == group[ind].id.ToString()) { attr2 = xnode.Attributes.GetNamedItem("surname"); attr2.Value = adu.surnameBox.Text; attr2 = xnode.Attributes.GetNamedItem("name"); attr2.Value = adu.nameBox.Text; attr2 = xnode.Attributes.GetNamedItem("patron"); attr2.Value = adu.patronBox.Text; attr2 = xnode.Attributes.GetNamedItem("birthday"); attr2.Value = adu.dateTimePicker1.Value.ToShortDateString(); xDoc.Save("data.xml"); UpdateUsersList(); break; } } } } } } }