public void RemoveElement(Apartment apartment) { Type fileType = apartment.GetType(); var reader = new XmlApartmentReader(this._filePath); _ = reader.Exist(apartment) ? "Delete this one" : throw new ArgumentException($"There is no the same {fileType.Name}."); XmlElement xRoot = this.Xml.DocumentElement; XmlNode child = xRoot.SelectSingleNode(fileType.Name + $"[Id='{fileType.GetProperty("Id").GetValue(apartment)}']"); this.Xml.DocumentElement.RemoveChild(child); this.Xml.Save(this._filePath); }
public void AppendElement(Apartment apartment) { Type fileType = apartment.GetType(); XmlApartmentReader reader = new XmlApartmentReader(this._filePath); _ = reader.Exist(apartment) ? throw new ArgumentException($"The same {fileType.Name} already exists.") : "Create new one"; XmlElement newElement = this.Xml.CreateElement(string.Empty, fileType.Name, string.Empty); _ = this.Xml.DocumentElement?.AppendChild(newElement) ?? throw new ArgumentException("There is no root in xml file"); foreach (var propertyInfo in fileType.GetProperties()) { XmlElement child = this.Xml.CreateElement(string.Empty, propertyInfo.Name, string.Empty); XmlText childText = this.Xml.CreateTextNode(propertyInfo.GetValue(apartment).ToString()); child.AppendChild(childText); newElement.AppendChild(child); } this.Xml.Save(this._filePath); }