public bool Save(string FileName) { if (!AnyTaskSelected || FileName == null) { return(false); } XDocument doc = new XDocument(new XElement("task", SelectedTask.Attribute("number"), new XElement("table", DataTable .Skip(1) .Select(i => new XElement("row", i .Select(j => new XElement("cell", new XAttribute("value", j.Text)) )))))); doc.Save(FileName); return(true); }
public bool Load(string FileName) { if (!AnyTaskSelected) { return(false); } if (!System.IO.File.Exists(FileName)) { throw new XMLFileOpenSaveException("File doesn't exist!"); } XDocument doc = XDocument.Load(FileName); #region Validation var task = doc.Element("task"); if (task == null) { throw new XMLValidationException("XML file has no element <task>"); } if (task.Attribute("number").Value != SelectedTask.Attribute("number").Value) { return(false); } var table = task.Element("table"); if (table == null) { throw new XMLValidationException("XML file has no element <table> in <task> section"); } var rows = table.Descendants("row").ToArray(); if (rows.Length != DataTable.Count - 1) { throw new XMLValidationException("Element <table> doesn't match selected task table"); } foreach (var row in rows) { var cells = row.Descendants("cell"); if (cells.Count() != DataTable.First().Count) { throw new XMLValidationException("Element <table> doesn't match selected task table"); } foreach (var cell in cells) { if (cell.Attribute("value") == null) { throw new XMLValidationException("Element <cell> has no attribute 'value'"); } } } #endregion for (int i = 0; i < rows.Length; i++) { var cells = rows[i].Descendants("cell").ToArray(); for (int j = 0; j < cells.Length; j++) { DataTable[i + 1][j].Text = cells[j].Attribute("value").Value; } } return(true); }