|
#region using
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
#endregion using
namespace VvmScaffolding
{
public class AppSetting
{
const string ConfigFile = "ServiceReferences.ClientConfig";
#region 'base' GetAppSetting
/// <summary>
/// using an XML Reader enumerates the <add ..../> elements till it finds the one
/// with the right 'key'
/// </summary>
/// <param name="strKey">the key</param>
/// <returns>the value for that key</returns>
static public string GetAppSetting(string strKey)
{
string strValue = string.Empty;
var settings = new XmlReaderSettings();
settings.XmlResolver = new XmlXapResolver();
var reader = XmlReader.Create(ConfigFile);
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "add")
{
if (reader.HasAttributes)
{
string fileKey = reader.GetAttribute("key");
if ((string.IsNullOrEmpty(fileKey) == false) && (fileKey == strKey))
{
strValue = reader.GetAttribute("value");
break;
}
}
}
}
}
return strValue;
}
#endregion 'base' GetAppSetting
#region type specific GetAppSetting
/// <summary>
/// get the value for the specified key. if the key is not there returns the specified default value
/// </summary>
/// <param name="key">the specified key</param>
/// <param name="defaultVal">the default value</param>
/// <returns>the value found or the passed-in default value</returns>
static public T GetAppSetting<T>(string key, T defaultVal)
{
T result = defaultVal;
try
{
string sVal = GetAppSetting(key);
if (string.IsNullOrEmpty(sVal) == false)
{
result = (T)Convert.ChangeType(sVal, typeof(T), new CultureInfo("en-US"));
}
}
catch
{
result = defaultVal;
}
return result;
}
#endregion type specific GetAppSetting
#region GetMenuItems
/// <summary>
/// using an XML Reader enumerates all the <MenuItem ..../> elements
/// for each one it create and populate a class that contains the
/// Name View and ViewModel and adds it to a list.
/// </summary>
/// <returns>a list that contains all the triplets</returns>
static public List<ThreeSringHolder> GetMenuItems()
{
var list = new List<ThreeSringHolder>();
var settings = new XmlReaderSettings();
settings.XmlResolver = new XmlXapResolver();
var reader = XmlReader.Create(ConfigFile);
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "MenuItem")
{
string d = reader.GetAttribute("Name");
string v = reader.GetAttribute("View");
string vm = reader.GetAttribute("ViewModel");
var item = new ThreeSringHolder(d, v, vm);
list.Add(item);
}
else
{
if (list.Count > 0)
{
break;
}
}
}
}
return list;
}
#endregion GetMenuItems
}
#region ThreeSringHolder class
public class ThreeSringHolder
{
public string Name { get; set; }
public string View { get; set; }
public string ViewModel { get; set; }
public ThreeSringHolder()
{
Name = string.Empty;
View = string.Empty;
ViewModel = string.Empty;
}
public ThreeSringHolder(string d, string v, string vm)
{
Name = d;
View = v;
ViewModel = vm;
}
}
#endregion ThreeSringHolder class
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.