Beispiel #1
0
        public void RemoveElement(Booking booking)
        {
            Type fileType = booking.GetType();

            var reader = new XmlBookingReader(this._filePath);

            _ = reader.Exist(booking) ? "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(booking)}']");

            this.Xml.DocumentElement.RemoveChild(child);

            this.Xml.Save(this._filePath);
        }
Beispiel #2
0
        public void AppendElement(Booking booking)
        {
            Type fileType = booking.GetType();

            XmlBookingReader reader = new XmlBookingReader(this._filePath);

            _ = reader.Exist(booking) ? 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(booking).ToString());
                child.AppendChild(childText);
                newElement.AppendChild(child);
            }

            this.Xml.Save(this._filePath);
        }