예제 #1
0
        private static T Read <T>(Stream reader) where T : new()
        {
            using (var xmlReader = XmlReader.Create(reader, _readerSettings))
            {
                var type = typeof(T);

                var sectionName = ConfigurationSectionAttribute.Name(type);

                try
                {
                    if (!xmlReader.ReadToDescendant(sectionName))
                    {
                        // There is no section with this name in the file so return an empty one
                        return(new T());
                    }
                }
                catch (XmlException)
                {
                    // Most likely caused by "Root element is missing"
                    return(new T());
                }

                var serializer = new XmlSerializer(type, type.Get <ConfigurationSectionAttribute>());
                return((T)serializer.Deserialize(xmlReader.ReadSubtree()));
            }
        }
예제 #2
0
        public void DeleteSection <T>()
        {
            var sectionName = ConfigurationSectionAttribute.Name(typeof(T));

            var doc = OpenDocument();

            DeleteSection(doc, sectionName);
            WriteDocument(doc);
        }
예제 #3
0
        private static XmlNode CreateElementFromSection <T>(T section)
        {
            var sectionDoc = new XmlDocument();

            sectionDoc.LoadXml(XmlSerializer <T> .ToString(section));

            var element = sectionDoc.DocumentElement;

            element.RemoveAttribute("xmlns:xsi");
            element.RemoveAttribute("xmlns:xsd");
            element.SetAttribute(_versionAttributeName, ConfigurationSectionAttribute.Version(typeof(T)));
            return(element);
        }