Exemplo n.º 1
0
        /// <summary>
        /// Create a live settings store attached to a settings file.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static SettingsStore <T> Create(string path)
        {
            var document = new XmlDocument();

            var directory = Path.GetDirectoryName(path);
            var file      = Path.GetFileName(path);
            var watcher   = new FileSystemWatcher(directory, file);

            try
            {
                var xml = File.ReadAllText(path, _xmlEncoding);
                document.LoadXml(xml);
            }
            catch
            {
                // Errors can be ignored.
                document = new XmlDocument();
                document.LoadXml("<" + typeof(T).Name + "/>");
            }

            T settings;

            try
            {
                settings = SettingsSerializer.FromXmlDocument <T>(document);
            }
            catch
            {
                settings = SettingsSerializer.CreateDefault <T>();
            }

            var store = new SettingsStore <T>(document, settings, path, watcher);

            return(store);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create store from an xml string.
        /// </summary>
        /// <param name="xmlString">The XML.</param>
        /// <returns>The object managing the settings.</returns>
        public static SettingsStore <T> CreateFromXml(string xmlString)
        {
            var document = new XmlDocument();

            document.LoadXml(xmlString);

            var settings = SettingsSerializer.FromXmlDocument <T>(document);
            var store    = new SettingsStore <T>(document, settings);

            return(store);
        }