Most of the time you need to display messages to
the user which represents the state of their request and action that happened to
the request. Messages Like "Data has been inserted", "User has been
deleted" etc. It is always a good idea to put all those messages in an
XML file so you can easily change it in the future. One other
advantage of using the XML file is that you don't need to build your application
if you change any message. Here is a little code snippet I wrote that returns
the message from the xml file based on the elementName.
In the code below I am using the XmlDocument object which knows the structure
of the XML document. On the other hand if you use XMLTextReader object to read
the element name then you need to go through all the elements until you find the
correct element name. I highly recommend using XMLDocument for this scenario.
private const string MESSAGES_FILE_PATH = "~/Messages/UI_Messages.xml";
private const string XML_DOCUMENT = "XmlDocument";
public static string GetMessageByElementName(string elementName)
{
string message = String.Empty;
XmlDocument xmlDoc = null;
if (HttpContext.Current.Cache[XML_DOCUMENT] == null)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath
(MESSAGES_FILE_PATH));
// insert into cache object
HttpContext.Current.Cache.Insert(XML_DOCUMENT,
xmlDoc, new System.Web.Caching.CacheDependency
(HttpContext.Current.Server.MapPath(MESSAGES_FILE_PATH)));
}
// retrieves the value from the cache object
xmlDoc = (XmlDocument)HttpContext.Current.Cache[XML_DOCUMENT];
message = ((XmlNode)((XmlNodeList)xmlDoc
.GetElementsByTagName(elementName)).Item(0)).InnerText;
return message;
}
The XML file will look something like this:
<Messages>
<RegisterationSuccessfull>
Your account has been created successfully.
</RegisterationSuccessfull>
<RegisterationFailure>
This account is already registered.
</RegisterationFailure>
<CategoryAddedSuccess>
New Category has been added.
</CategoryAddedSuccess>
<CategoryAddedFailure>
Error: Category has not been added.
</CategoryAddedFailure>
<FileAlreadyExists>
Error: This file already exists.
</FileAlreadyExists>
</Messages>
powered by IMHO 1.3