Geeks With Blogs

AzamSharp Some day I will know everything. I hope that day never comes.

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

Posted on Tuesday, May 30, 2006 9:01 PM | Back to top


Comments on this post: Saving User Interface Messages in an XML File

# re: Saving User Interface Messages in an XML File
Requesting Gravatar...
Hai

That is really a Good Tip.

It will help a lot of people if they follow it.

it is also a natural extension for Internationalizing the Application.
Left by Good Tip on May 31, 2006 3:56 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net