Пример #1
0
        public void RemoveElement(GuestHistory guestHistory)
        {
            Type fileType = guestHistory.GetType();

            var reader = new XmlGuestHistoryReader(this._filePath);

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

            this.Xml.DocumentElement.RemoveChild(child);

            this.Xml.Save(this._filePath);
        }
Пример #2
0
        public void AppendElement(GuestHistory guestHistory)
        {
            Type fileType = guestHistory.GetType();

            XmlGuestHistoryReader reader = new XmlGuestHistoryReader(this._filePath);

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

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