public void RemoveXmlRecord(IBaseRecord rec) { XElement xElement = XElement.Load(_path); FindElementByRecord(xElement.Elements(), rec)?.Remove(); xElement.Save(_path); }
private XmlElement CreateRecordElement(IBaseRecord rec) { XmlElement record = _document.CreateElement("Record"); record.SetAttribute("Type", rec.Type.ToString()); XmlElement data = _document.CreateElement("Data"); record.AppendChild(data); data.SetAttribute("Date", rec.Date.ToString(CultureInfo.InvariantCulture)); if (rec.Description == null) { rec.Description = ""; } data.SetAttribute("Description", rec.Description); switch (rec.Type) { case ERecordType.Hours: data.SetAttribute("WorkTimeFrom", ((IHoursRecord)rec).WorkTimeFrom.ToString()); data.SetAttribute("WorkTimeTo", ((IHoursRecord)rec).WorkTimeTo.ToString()); data.SetAttribute("BreakTime", ((IHoursRecord)rec).BreakTime.ToString()); data.SetAttribute("OverTime", ((IHoursRecord)rec).OverTime.ToString()); break; case ERecordType.Pieces: data.SetAttribute("Variable", ((IPiecesRecord)rec).Pieces.ToString()); break; } if (rec.Type != ERecordType.Vacation && rec.Type != ERecordType.None) { IRecord baseRec = (IRecord)rec; data.SetAttribute("Price", baseRec.Price.ToString(CultureInfo.InvariantCulture)); data.SetAttribute("Bonus", baseRec.Bonus.ToString(CultureInfo.InvariantCulture)); } return(record); }
private IBaseRecord ParsePiecesAndTimeRecord(XElement recordElement) { IBaseRecord rec = null; Enum.TryParse(recordElement.Attribute("Type")?.Value, out ERecordType recType); XElement dataElement = recordElement.Element("Data"); if (dataElement != null) { DateTime date = ParseDate(dataElement.Attribute("Date")?.Value); string description = dataElement.Attribute("Description")?.Value; var(price, bonus) = ParseIRecord(recType, dataElement.Attribute("Price")?.Value, dataElement.Attribute("Bonus")?.Value); switch (recType) { case ERecordType.Hours: TimeSpan workTimeFrom = ParseTimeRecord(dataElement.Attribute("WorkTimeFrom")?.Value.Split(':')).ToTimeSpan(); TimeSpan workTimeTo = ParseTimeRecord(dataElement.Attribute("WorkTimeTo")?.Value.Split(':')).ToTimeSpan(); TimeSpan breakTime = ParseTimeRecord(dataElement.Attribute("BreakTime")?.Value.Split(':')) .ToTimeSpan(); WorkTime overTime = ParseTimeRecord(dataElement.Attribute("OverTime")?.Value.Split(':')); rec = new HoursRecord(date, workTimeFrom, workTimeTo, price, bonus, description, overTime, new WorkTime(breakTime.Hours, breakTime.Minutes)); break; case ERecordType.Pieces: uint.TryParse(dataElement.Attribute("Variable")?.Value, out uint pieces); rec = new PiecesRecord(date, pieces, price, bonus, description); break; case ERecordType.Vacation: rec = new VacationRecord(date, description); break; } } return(rec); }
public static void PutObj(object obj) { // item 378 IBaseRecord rec = obj as IBaseRecord; // item 380 if (rec == null) { // item 418 if (obj == null) { // item 383 Put("null"); } else { // item 421 Console.WriteLine("{0} {1}", obj.GetType().Name, obj); } } else { // item 379 Console.WriteLine("{0} {1}", rec.GetType().Name, rec.Id); } }
private XElement FindElementByRecord(IEnumerable <XElement> records, IBaseRecord rec) { foreach (XElement recordElement in records) { IBaseRecord foundRec = ParsePiecesAndTimeRecord(recordElement); if (rec.Type == foundRec.Type) { if (IfFoundedElementIsEquals(rec, foundRec)) { return(recordElement); } } } return(null); }
public void EditXmlRecord(IBaseRecord oldRec, IBaseRecord newRecord) { XElement root = XElement.Load(_path); XElement el = FindElementByRecord(root.Elements(), oldRec); if (el == null) { return; } XElement newEl = XElement.Parse(CreateRecordElement(newRecord).OuterXml); el.ReplaceWith( newEl ); root.Save(_path); }
public static void PutObj(object obj) { // item 328 IBaseRecord rec = obj as IBaseRecord; // item 330 if (rec == null) { // item 333 Put("{0}", obj); } else { // item 329 Console.WriteLine("{0} {1}", rec.GetType().Name, rec.Id); } }
private bool IfFoundedElementIsEquals(IBaseRecord rec, IBaseRecord foundRec) { switch (rec.Type) { case ERecordType.Hours: return((HoursRecord)foundRec == (HoursRecord)rec); case ERecordType.Pieces: return((PiecesRecord)foundRec == (PiecesRecord)rec); case ERecordType.Vacation: return((VacationRecord)foundRec == (VacationRecord)rec); case ERecordType.None: break; default: throw new ArgumentOutOfRangeException(); } return(false); }
public void AppendXmlFile(IBaseRecord rec) { try { _document.Load(_path); } catch (XmlException) { //TODO DIALOG } catch (FileNotFoundException) { using (XmlWriter writer = XmlWriter.Create(_path)) { CreateRootElement(writer); } _document.Load(_path); } catch (IOException) { return; //TODO DIALOG } catch (Exception e) { Console.WriteLine(e); throw; } _document.DocumentElement?.AppendChild(CreateRecordElement(rec)); try { _document.Save(_path); } catch (XmlException e) { Console.WriteLine(e); throw; } }