|
using System;
using System.Data;
using System.Web;
using System.Web.Caching;
namespace MyControls
{
/// <summary>
/// Class for returning resource strings from an XML file
/// </summary>
public class XMLResource
{
/// <summary>
/// Get a DataSet with resource strings
/// </summary>
/// <returns>DataSet with resource strings</returns>
private DataSet GetResourceDS()
{
//Try to get the DataSet from the Cache first
//Note that this one bombs at design time, (we have no HttpContext)
//but this exception is caught in the custom control
DataSet ds = (DataSet)HttpContext.Current.Cache[HttpContext.Current.Request.CurrentExecutionFilePath];
if(ds == null)
{
//no DataSet in the Cache, get a new one and store it in the cache
//get the text-id from the resource file for our web-page
ds = new DataSet("resourceStrings");
//build the name of the xml file from the name of the web page we're sitting in
String fileName = HttpContext.Current.Request.CurrentExecutionFilePath;
fileName = fileName.Substring(0, fileName.LastIndexOf(".")) + ".xml";
try
{
//read ds from the xml file
ds.ReadXml(HttpContext.Current.Server.MapPath(fileName));
}
catch (System.IO.FileNotFoundException)
{
//xml file not found, we will return the empty DataSet
}
//put in Cache with dependency to the xml file
HttpContext.Current.Cache.Insert(HttpContext.Current.Request.CurrentExecutionFilePath,
ds, new CacheDependency(HttpContext.Current.Server.MapPath(fileName)));
}
return ds;
}
/// <summary>
/// Get the value from the XML resource
/// </summary>
/// <param name="key">The ID of the value to get</param>
/// <param name="defaultValue">The default value to return if the key is not found</param>
public String GetValueFromKey(String key, String defaultValue)
{
DataSet ds = GetResourceDS();
//filter out everything except our resource string in a dataview
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "key = '" + key + "'";
if(dv.Count > 0) //key found, show the value
return dv[0]["value"].ToString();
else //key not found return default value
return defaultValue;
}
}
}
|
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 article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
Johan started coding a long time ago on a ZX Spectrum and the Z80 processor. His parents were rather sceptical about him sitting up all night coding and playing Manic Miner but they don't regret buying that computer now, since it turned out pretty good in the end.
Johan is now working as a Systems Architect and jump between a number of different platforms and computer languages every day. He wouldn’t cope if it weren’t for IntelliSense and code completion...