コード例 #1
0
        public void RemoveElement(ApartmentCategory apartmentCategory)
        {
            Type fileType = apartmentCategory.GetType();

            var reader = new XmlApartmentCategoryReader(this._filePath);

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

            this.Xml.DocumentElement.RemoveChild(child);

            this.Xml.Save(this._filePath);
        }
コード例 #2
0
        public void AppendElement(ApartmentCategory apartmentCategory)
        {
            Type fileType = apartmentCategory.GetType();

            XmlApartmentCategoryReader reader = new XmlApartmentCategoryReader(this._filePath);

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

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