Как сохранить данные в XML? - C#
Формулировка задачи:
Загружаю данные из XML в DataGrid. Некие действия с данными после чего мне нужно новые данные сохранить, пробывал вот так
Но ничего не получилось, как работать с коллекциями не знаю, еще не проходили, но я так понимаю что с помощью неё нужно как-то данные сохранять, теги и атрибут создаются, но вот новые данные не записываются
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
List<Receipt> newReceipt = new List<Receipt>();
XmlDocument doc = new XmlDocument();
//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
//(2) string.Empty makes cleaner code
XmlElement element1 = doc.CreateElement(string.Empty, "Receipts", string.Empty);
doc.AppendChild(element1);
XmlElement element2 = doc.CreateElement(string.Empty, "Receipt", string.Empty);
element1.AppendChild(element2);
XmlAttribute attribute = doc.CreateAttribute("Data");
attribute.Value = XmlConvert.ToString(newReceipt.Date);
element2.Attributes.Append(attribute);
XmlElement element3 = doc.CreateElement(string.Empty, "Number", string.Empty);
XmlText text1 = doc.CreateTextNode(newReceipt.Number);
element3.AppendChild(text1);
element2.AppendChild(element3);
XmlElement element4 = doc.CreateElement(string.Empty, "NameOperation", string.Empty);
XmlText text2 = doc.CreateTextNode(newReceipt.NameOperation);
element4.AppendChild(text2);
element2.AppendChild(element4);
XmlElement element5 = doc.CreateElement(string.Empty, "Cost", string.Empty);
XmlText text3 = doc.CreateTextNode(newReceipt.Cost);
element5.AppendChild(text3);
element2.AppendChild(element5);
doc.Save("D:\\document.xml");
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1
{
public class ParsedXmlDocument
{
//Свойства класса
public List<Receipt> Receipts { get; set; }
//Свойства класса
public ParsedXmlDocument()
{
this.Receipts = new List<Receipt>();
}
}
public class Receipt
{
//Свойства класса
public string Number { get; set; }
public string NameOperation { get; set; }
public DateTime Date { get; set; }
public string Cost { get; set; }
//Свойства класса
public Receipt()
{
this.Number = string.Empty;
this.NameOperation = string.Empty;
this.Date = Date;
this.Cost = string.Empty;
}
}
}Решение задачи: «Как сохранить данные в XML?»
textual
Листинг программы
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement(nameof(Receipts));
xmlDoc.AppendChild(rootNode);
foreach (var item in Receipts)
{
var userNode = xmlDoc.CreateElement(nameof(Receipt));
var attribute = xmlDoc.CreateAttribute(nameof(item.Date));
attribute.Value = item.Date.ToString();
userNode.Attributes.Append(attribute);
var subNode = xmlDoc.CreateElement(nameof(item.Number));
subNode.InnerText = item.Number.ToString();
userNode.AppendChild(subNode);
subNode = xmlDoc.CreateElement(nameof(item.NameOperation));
subNode.InnerText = item.NameOperation.ToString();
userNode.AppendChild(subNode);
subNode = xmlDoc.CreateElement(nameof(item.Cost));
subNode.InnerText = item.Cost.ToString();
userNode.AppendChild(subNode);
rootNode.AppendChild(userNode);
}
xmlDoc.Save("XML123.xml");