PageRenderTime 93ms CodeModel.GetById 83ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Providers/XmlProvider/Settings.cs

#
C# | 81 lines | 49 code | 12 blank | 20 comment | 4 complexity | 17839942745d271bf9d141a67d5a5c8c MD5 | raw file
 1namespace BlogEngine.Core.Providers
 2{
 3    using System;
 4    using System.Collections.Specialized;
 5    using System.Web;
 6    using System.Web.Configuration;
 7    using System.Xml;
 8
 9    /// <summary>
10    /// A storage provider for BlogEngine that uses XML files.
11    ///     <remarks>
12    /// To build another provider, you can just copy and modify
13    ///         this one. Then add it to the web.config's BlogEngine section.
14    ///     </remarks>
15    /// </summary>
16    public partial class XmlBlogProvider : BlogProvider
17    {
18        #region Public Methods
19
20        /// <summary>
21        /// Loads the settings from the provider.
22        /// </summary>
23        /// <returns>A StringDictionary.</returns>
24        public override StringDictionary LoadSettings()
25        {
26            var filename = HttpContext.Current.Server.MapPath(string.Format("{0}settings.xml", Blog.CurrentInstance.StorageLocation));
27            var dic = new StringDictionary();
28
29            var doc = new XmlDocument();
30            doc.Load(filename);
31
32            var settings = doc.SelectSingleNode("settings");
33            if (settings != null)
34            {
35                foreach (XmlNode settingsNode in settings.ChildNodes)
36                {
37                    var name = settingsNode.Name;
38                    var value = settingsNode.InnerText;
39
40                    dic.Add(name, value);
41                }
42            }
43
44            return dic;
45        }
46
47        /// <summary>
48        /// Saves the settings to the provider.
49        /// </summary>
50        /// <param name="settings">
51        /// The settings.
52        /// </param>
53        public override void SaveSettings(StringDictionary settings)
54        {
55            if (settings == null)
56            {
57                throw new ArgumentNullException("settings");
58            }
59
60            var filename = string.Format("{0}settings.xml", this.Folder);
61            var writerSettings = new XmlWriterSettings { Indent = true };
62
63            // ------------------------------------------------------------
64            // Create XML writer against file path
65            // ------------------------------------------------------------
66            using (var writer = XmlWriter.Create(filename, writerSettings))
67            {
68                writer.WriteStartElement("settings");
69
70                foreach (string key in settings.Keys)
71                {
72                    writer.WriteElementString(key, settings[key]);
73                }
74
75                writer.WriteEndElement();
76            }
77        }
78
79        #endregion
80    }
81}