[图片][图片]Load()
/// <summary>
/// Initializes the singleton instance of the <see cref="BlogSettings"/> class.
/// </summary>
private void Load()
{
//------------------------------------------------------------
// Local members
//------------------------------------------------------------
string settingsFilePath = String.Empty;
XmlReaderSettings readerSettings;
XmlDocument settingsDocument;
Type settingsType;
//------------------------------------------------------------
// Attempt to load configured settings
//------------------------------------------------------------
try
{
//------------------------------------------------------------
// Get settings location and instance type
//------------------------------------------------------------
settingsFilePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["SettingsFile"]);
settingsType = this.GetType();
//------------------------------------------------------------
// Define XML reader settings
//------------------------------------------------------------
readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
readerSettings.IgnoreWhitespace = true;
//------------------------------------------------------------
// Load settings into XML document
//------------------------------------------------------------
settingsDocument = new XmlDocument();
using (FileStream stream = new FileStream(settingsFilePath, FileMode.Open, FileAccess.Read))
{
using (XmlReader reader = XmlReader.Create(stream, readerSettings))
{
settingsDocument.Load(reader);
}
}
//------------------------------------------------------------
// Enumerate through individual settings nodes
//------------------------------------------------------------
foreach (XmlNode settingsNode in settingsDocument.SelectSingleNode("settings").ChildNodes)
{
//------------------------------------------------------------
// Extract the setting's name/value pair
//------------------------------------------------------------
string name = settingsNode.Name;
string value = settingsNode.InnerText;
//------------------------------------------------------------
// Enumerate through public properties of this instance
//------------------------------------------------------------
foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
{
//------------------------------------------------------------
// Determine if configured setting matches current setting based on name
//------------------------------------------------------------
if (propertyInformation.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
{
//------------------------------------------------------------
// Attempt to apply configured setting
//------------------------------------------------------------
try
{
propertyInformation.SetValue(this, Convert.ChangeType(value, propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
}
catch
{
// TODO: Log exception to a common logging framework?
}
break;
}
}
}
}
catch
{
//------------------------------------------------------------
// Rethrow exception
//------------------------------------------------------------
throw;
}
}
[图片][图片]Save()
/// <summary>
/// Saves the settings to disk.
/// </summary>
public void Save()
{
//------------------------------------------------------------
// Local members
//------------------------------------------------------------
string settingsFilePath = String.Empty;
XmlWriterSettings writerSettings;
Type settingsType;
//------------------------------------------------------------
// Attempt to persist settings
//------------------------------------------------------------
try
{
//------------------------------------------------------------
// Get settings storage path and instance type
//------------------------------------------------------------
settingsFilePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["SettingsFile"]);
settingsType = this.GetType();
//------------------------------------------------------------
// Define XML writer settings used to generate output
//------------------------------------------------------------
writerSettings = new XmlWriterSettings();
writerSettings.Indent = true;
//------------------------------------------------------------
// Create XML writer against file path
//------------------------------------------------------------
using (XmlWriter writer = XmlWriter.Create(settingsFilePath, writerSettings))
{
//------------------------------------------------------------
// Write settings root element
//------------------------------------------------------------
writer.WriteStartElement("settings");
//------------------------------------------------------------
// Enumerate through settings properties
//------------------------------------------------------------
foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
{
//------------------------------------------------------------
// Attempt to write settings name/value pairs
//------------------------------------------------------------
try
{
//------------------------------------------------------------
// Verify not
//------------------------------------------------------------
if (propertyInformation.Name != "Instance")
{
//------------------------------------------------------------
// Extract property value and its string representation
//------------------------------------------------------------
object propertyValue = propertyInformation.GetValue(this, null);
string valueAsString = propertyValue.ToString();
//------------------------------------------------------------
// Format null/default property values as empty strings
//------------------------------------------------------------
if (propertyValue.Equals(null))
{
valueAsString = String.Empty;
}
if (propertyValue.Equals(Int32.MinValue))
{
valueAsString = String.Empty;
}
if (propertyValue.Equals(Single.MinValue))
{
valueAsString = String.Empty;
}
&n
添加评论 返回顶部 |
返回首页