Запись данных в XML файл из TextBox по нажатию кнопки - C#
Формулировка задачи:
Помогите пожалуйста. Как можно сделать запись строки в XML файл,в определенный тэг, из текстбокса?Если не сложно приведите пример кода
Решение задачи: «Запись данных в XML файл из TextBox по нажатию кнопки»
textual
Листинг программы
XDocument xDoc = new XDocument();
XElement xroot = new XElement("phones");
//Создаем первый элемент
XElement iphone = new XElement("phone");
XAttribute attIphone = new XAttribute("name", "IPhone6");
XElement icompany = new XElement("company", "Apple");
XElement iprice = new XElement("price","56000");
iphone.Add(attIphone);
iphone.Add(icompany);
iphone.Add(iprice);
//Второй элемент
XElement sony = new XElement("sony");
XAttribute attSony = new XAttribute("name","Sony Z5 Compact");
XElement scompany = new XElement("company", "Sony");
XElement sprice = new XElement("price","45000");
sony.Add(attSony);
sony.Add(scompany);
sony.Add(sprice);
xroot.Add(iphone);
xroot.Add(sony);
xDoc.Add(xroot);
xDoc.Save("phones.xml");
XDocument xDoc2 = new XDocument(new XElement("phones",
new XElement("phone",
new XAttribute("name", "IPhone6"),
new XElement("company", "Apple"),
new XElement("price", "58000")),
new XElement("phone",
new XAttribute("name", "Sony Z1"),
new XElement("company", "Sony"),
new XElement("price", "21300"))));
xDoc2.Save("phones2.xml");